Hello. I'm pretty new to perl. I have an xml file that has several different xml messages. What is common about each of the xml messages is that they each have a child node with the same name (EventInfo). The following is an example of the payloads:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DeliveryTimeChanged CurrentStatus="OnHold" xmlns:ns2="http://com/post/orderupdatesasync/jaxbxml"> <EventInfo EventId="666313444" CreationDatetime="2017/02/09 07:59:17 369 GMT" RequestId="321150454"> <TopicCounts TopicName="DELIVERY.TIME.CHANGED" TopicCount="1"/> </EventInfo> <DeliveryChangeOperationType OperationTypeCode="DELAY" OperationSubtypeCode="HOLD" DeliveryChangeReason="Weather" DeliveryDate="20170210"/> </DeliveryTimeChanged>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DeliveryRouteChanged CurrentStatus="OnHold" xmlns:ns2="http://com/post/orderupdatesasync/jaxbxml"> <EventInfo EventId="666313445" CreationDatetime="2017/02/09 07:59:23 639 GMT" RequestId="321150454"> <TopicCounts TopicName="DELIVERY.ROUTE.CHANGED" TopicCount="1"/> </EventInfo> <DeliveryRouteType OperationTypeCode="AIR" OperationSubtypeCode="HOLD" DeliveryDate="20170210"/> </DeliveryRouteChanged>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DeliveryCanceled CurrentStatus="Canceled" xmlns:ns2="http://com/post/orderupdatesasync/jaxbxml"> <EventInfo EventId="666313446" CreationDatetime="2017/02/09 07:59:44 963 GMT" RequestId="421150444"> <TopicCounts TopicName="DELIVERY.STATUS.CANCELED" TopicCount="1"/> </EventInfo> <DeliveryStatusType DeliveryStatusCode="CX" OperationSubtypeCode="CANCELED" DeliveryDate="20170210"/> </DeliveryCanceled>

What I would like is to pull the entire xml message that has a certain RequestId attribute value (321150454) in the EventInfo node regardless of what the parent node is.

I have tried the following perl script:

perl -ne ' if(/EventInfo>/){$p=0} if(/RequestId="321150454"/) {print $ +ARGV; print " "; print; $p=1;next}print if$p' sample.xml

The output is only giving me the EventInfo node:

<EventInfo EventId="666313444" CreationDatetime="2017/02/09 07:59:17 369 GMT" RequestId="321150454"> <TopicCounts TopicName="DELIVERY.TIME.CHANGED" TopicCount="1"/>

sample.xml <EventInfo EventId="666313445" CreationDatetime="2017/02/09 07:59:23 639 GMT" RequestId="321150454"> <TopicCounts TopicName="DELIVERY.ROUTE.CHANGED" TopicCount="1"/>

What I would like is the entire xml payload of these two records:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DeliveryTimeChanged CurrentStatus="OnHold" xmlns:ns2="http://com/post/orderupdatesasync/jaxbxml"> <EventInfo EventId="666313444" CreationDatetime="2017/02/09 07:59:17 369 GMT" RequestId="321150454"> <TopicCounts TopicName="DELIVERY.TIME.CHANGED" TopicCount="1"/> </EventInfo> <DeliveryChangeOperationType OperationTypeCode="DELAY" OperationSubtypeCode="HOLD" DeliveryChangeReason="Weather" DeliveryDate="20170210"/> </DeliveryTimeChanged>

AND

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DeliveryRouteChanged CurrentStatus="OnHold" xmlns:ns2="http://com/post/orderupdatesasync/jaxbxml"> <EventInfo EventId="666313445" CreationDatetime="2017/02/09 07:59:23 639 GMT" RequestId="321150454"> <TopicCounts TopicName="DELIVERY.ROUTE.CHANGED" TopicCount="1"/> </EventInfo> <DeliveryRouteType OperationTypeCode="AIR" OperationSubtypeCode="HOLD" DeliveryDate="20170210"/> </DeliveryRouteChanged>

How do I get the entire xml payload (parent and child nodes)? Any help with this would be greatly appreciated. Thanks for your time.


In reply to Retrieving XML From a File Based On Child Node Attribute by the_r

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.