Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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,

Cyril.

Comment on Nested data with XML::Simple
Select or Download Code
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.
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>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020037]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-08-23 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (171 votes), past polls