Solved

PHP Read XML Items

Posted on 2012-04-04
2
271 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 49
php56-php-mcrypt for rhel7 php56 1 54
Undefined variable problem 5 23
Inserting results of While loop to sql server 5 19
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
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.

828 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