Looping Through Nodes In An XML File Using PHP

JacobE

New Member
Hi Stackoverflow Community - Question from an enthusiast programmer that I've researched here a great deal but still can't figure out. Appreciate any help I can get.I have an XML document called example.xml with the following structure...\[code\]<Submissions> <Submission> <Name>Submission 001</Name> <Entries> <Entry> <EntryId>1</EntryId> <Field1>Foo</Field1> </Entry> <Entry> <EntryId>2</EntryId> <Field1>Bar</Field1> </Entry> </Entries> </Submission> <Submission> <Name>Submission 002</Name> <Entries> <Entry> <EntryId>1</EntryId> <Field1>Foo</Field1> </Entry> <Entry> <EntryId>2</EntryId> <Field1>Bar</Field1> </Entry> </Entries> </Submission></Submissions>\[/code\]I am using SimpleXML in PHP and would like parse the XML document and echo to the browser something that looks like this:\[code\]Submission 1, Entry 1, FooSubmission 1, Entry 2, BarSubmission 2, Entry 1, FooSubmission 2, Entry 2, Bar\[/code\]So basically the idea is just to loop through the XML document and associating every entry with the submission to which it belongs.I did my research and I'm comfortable getting the entries outputted with the following code, but I can't figure out how to associate the entries with a submission.\[code\]<?php$xml = simplexml_load_file('example.xml');$Submissions = $xml;foreach ($Submissions->Submission->Entries->Entry as $Entry) { echo $Entry->EntryId.' , '.$Entry->Field2.' <br />';}?>\[/code\]Appreciate your help!
 
Back
Top