Solved

PHP Read XML Items

Posted on 2012-04-04
2
268 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
ID: 37808211
$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
ID: 37808277
Perfect!
Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 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

14 Experts available now in Live!

Get 1:1 Help Now