Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Nested data with XML::Simple

by cyrilw (Initiate)
on Feb 22, 2013 at 01:04 UTC ( #1020037=perlquestion: print w/replies, xml ) Need Help??
cyrilw has asked for the wisdom of the Perl Monks concerning the following question:

We are sent XML orders that have this structure..

<OrderDetail> <ListOfItemDetail> <ItemDetail> <BuyerLineItemNum>1</BuyerLineItemNum> <ListOfStructuredNote> <StructuredNote> <GeneralNote>Line 1 Text.</GeneralNote> </StructuredNote> </ListOfStructuredNote> </ItemDetail> <ItemDetail> <BuyerLineItemNum>2</BuyerLineItemNum> <ListOfStructuredNote> <StructuredNote> <GeneralNote>Line 2 Text.</GeneralNote> </StructuredNote> </ListOfStructuredNote> <ListOfStructuredNote> <StructuredNote> <GeneralNote>More Line 2 Text.</GeneralNote> </StructuredNote> </ListOfStructuredNote> </ItemDetail> </ListOfItemDetail> </OrderDetail>

We use these bits of perl code to extract the data.

my ( $orderDetail ) = $message->{OrderDetail}; my ( $itemDetails ) = $orderDetail->{ListOfItemDetail}->{ItemDetail +}; $LineNoteNum = scalar ( @$itemDetails ); for ( $itemNum = 0; $itemNum < $LineNoteNum; $itemNum++ ) { my ( $lineItemNote ) = $$itemDetails[$itemNum]{ListOfStructu +redNote}->{StructuredNote}; for ( $NoteNum = 0; $NoteNum < $LineNoteNum; $NoteNum++ ) { my ( $testString ) = $$lineItemNote[$NoteNum]{GeneralNote}; } }

We only get the first line of Text for each ItemDetail.

I suspect that is because that is all I am asking for.

How do I get it to loop through all the text lines sent.?

Many thanks,


Replies are listed 'Best First'.
Re: Nested data with XML::Simple
by manorhce (Beadle) on Feb 22, 2013 at 05:47 UTC

    Hi Please try this and it will help you and please try to print the data accordingly from the complex data XML::Simple will pass the xml data and will create a nested data structure and you can bring your data from that

    #!/usr/bin/perl use XML::Simple; local $/ = undef; $xml_data = <DATA>; $xml_simple = XML::Simple->new( KeepRoot => 1, KeyAttr => 1, ForceArray => 1 ); $xml_obj_data = $xml_simple->XMLin($xml_data); use Data::Dumper; print Dumper($xml_obj_data); __DATA__ <OrderDetail> <ListOfItemDetail> <ItemDetail> <BuyerLineItemNum>1</BuyerLineItemNum> <ListOfStructuredNote> <StructuredNote> <GeneralNote>Line 1 Text.</GeneralNote> </StructuredNote> </ListOfStructuredNote> </ItemDetail> <ItemDetail> <BuyerLineItemNum>2</BuyerLineItemNum> <ListOfStructuredNote> <StructuredNote> <GeneralNote>Line 2 Text.</GeneralNote> </StructuredNote> </ListOfStructuredNote> <ListOfStructuredNote> <StructuredNote> <GeneralNote>More Line 2 Text.</GeneralNote> </StructuredNote> </ListOfStructuredNote> </ItemDetail> </ListOfItemDetail> </OrderDetail>
Re: Nested data with XML::Simple
by Anonymous Monk on Feb 22, 2013 at 01:14 UTC
    How do you read the file?

      Is this the bit you wanted.?

      my ( $xmlFile ) = $ARGV[0]; my ( $xs1 ) = XML::Simple->new(); my ( $message ) = $xs1->XMLin( $xmlFile, KeyAttr => [], ForceArray => +[ 'StructuredNote', 'ItemDetail', 'ItemDetail->StructuredNote' ] );

        Yes, but not really

        If you use that, then  my ( $orderDetail ) = $message->{OrderDetail}; is undef, so no loops will ever get entered

        But you say the loop does get entered, so I offer you How do I post a question effectively? , because typos and pseudocode don't work as debugging methods

        Cyril, your code won't run as you have presented it - try it yourself. Correct it and then re-post.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020037]
Approved by vinoth.ree
[Eily]: the French took it Discipulus
[Eily]: English copied French's habit of having a polite singular you, and lost thou through the ages :D

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2017-11-23 11:21 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (333 votes). Check out past polls.