简体   繁体   中英

Dynamic xml handling with php

I work with many different types of xml files. I load the contents of these into my mysql database. Problem is that i need to define the tags I want to pick everytime.

Is there a php dom object functions that can iterate over all the tags and give them to me.

this is my sample xml

<products> 
   <product> 
        <name>Name of product</name> 
        <categories> 
            <category>Apparel</category> 
           <category>Trousers</category> 
           <category>Blue</category>
        </categories> 
        <description>Blue trousers</description>
        <price>599.00</price> <regularPrice>599.00</regularPrice>
   </product>
</products>

Output should be NOT the values but the acctual name of the XML tags, in this case it should be Products, Product, Name, Categories, category, description, price

Getting those values I could dynamicly point them via a connection table to always be save in the right table and in the right field.

* Try this code it will work as expected *

$xmlD = '
 <products> 
   <product> 
        <name>Name of product</name> 
        <categories> 
            <category>Apparel</category> 
           <category>Trousers</category> 
           <category>Blue</category>
        </categories> 
        <description>Blue trousers</description>
        <price>599.00</price> <regularPrice>599.00</regularPrice>
   </product>
</products>
';
$xml = simplexml_load_string($xmlD);

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
  {
  echo $child->getName(). "<br />";
  foreach($child->children() as $innerChild):
      echo $innerChild->getName(). "<br />";
  endforeach;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM