Solved

PHP Read XML Items

Posted on 2012-04-04
2
264 Views
Last Modified: 2012-04-04
Hi all,

I'm loading a XML this way... using PHP

$xml = simplexml_load_file(PATH);

I can get any member by calling

$param = (string)$xml->Node->Child1;

No problem... i can read Child1, Child2, etc...

But, when i have multiple nodes with the same name...

Example

<det nItem="1">
<prod>
  <child1>HELLO</child1>
  <child2>WORLD</child2>
</prod>
</det>
<det nItem="2">
<prod>
  <child1>ITS ME</child1>
  <child2>DET AGAIN!</child2>
</prod>
</det>
<det nItem="3">
<prod>
  <child1>ITS ME</child1>
  <child2>AGAIN AGAIN!</child2>
</prod>
</det>

Ok, we have multiple "det" nodes... differentiated by "nItem" property

How to walk for every "det" nodes getting its content?

$param = (string)$xml->det?!->Child1;

Forget about "node" and "child"... my english is poor sorry...

regards,
0
Comment
Question by:cebasso
2 Comments
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
Comment Utility
$xml = simplexml_load_file(...);

foreach ($xml->det as $value)
{ 
  echo $value->prod->child1.'<br \>';
  echo $value->prod->child2.'<br \><br \>';
}

Open in new window


Bye, Olaf.
0
 
LVL 4

Author Closing Comment

by:cebasso
Comment Utility
Perfect!
Thank you!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now