how to form a path to XML subnodes with a multidimensional array

Where do I begin... a XML file needs to go into a database. Therefore I want to make a config array containing the mapping between XML nodes and table-columns of one table.

$maps = array(
 // 'node-name'=>'column-name'
 'prod_id'=>'supplier_product_id',
 'description'=>'product_description',
);

$xml=simplexml_load_file($file);

//just a test
foreach ($maps as $node => $col){
 echo 'node ' . $xml->$node . ' is mapped to: ' . $col; //this works
}

There is information I need to put in this (same) table, from a subnode. So I was thinking of putting subnodes in a nested array like this:

$maps = array(
 // 'node-name'=>'column-name'
 'prod_id'=>'supplier_product_id',
 'description'=>'product_description',
 // to access $xml->node->subnode;
 'category'=>array(
  'id'=>'category_id',
 ),
);

But now I get confused, how can I use the nested array to make an path to the node like this: $xml->category->id

I am a newbee in PHP and hopefully some help will keep me on the road again. All help is welcome, thank you in advance.

Answers


Try this:

<?php
$maps = array(
 // 'node-name'=>'column-name'
 'prod_id'=>'supplier_product_id',
 'description'=>'product_description',
 // to access $xml->node->subnode;
 'category'=>array(
  'id'=>'category_id'
 )
);

function getDataMapping( $maps, $child="" ) {
    global $file;

    $xml=simplexml_load_file($file);

    foreach ($maps as $node => $col) {
        if( is_array( $col ) ) {
            getDataMapping( $col, $node );
        } else {
            if( $child ) {
                echo 'node ' . $xml->{$child}->$node . ' is mapped to: ' . $col; //this works
            } else {
                echo 'node ' . $xml->$node . ' is mapped to: ' . $col; //this works
            }
        }
    }
}

getDataMapping( $maps );
?>

Obviously, if your nesting runs many levels deep (array within array and so on), you can change it to a recursive function.

Hope this helps.


Here's the XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<message>
<errorcode>100</errorcode>
<body>
 <jobs>
  <job>
   <id>1</id>
   <description>Nice job at the office</description>
   <hours>40</hours>
   <contact>
    <id>SYL</id>
    <name>Sylvia</name>
    <email>sylvia@mail.com</email>
   </contact>
  </job>
  <job>
   <id>2</id>
   <description>Construction work</description>
   <hours>32</hours>
   <contact>
    <id>HEN</id>
    <name>Hendrik</name>
    <email>hendrik@mail.com</email>
   </contact>
  </job>
 </jobs>
</body>
<attachements>
</attachements>
<filenames>
</filenames>
</message>

I just realize there is actually no need to read more than 2 levels. So your answer is enough, because it can access both $xml->node and $xml->child->node . With xpath('//job') I can set a base node entrance to all the nodes and iterate over them.


Need Your Help

Console warning not displayed when performing exit in node.js

javascript node.js console exit

I am new to Node.js. I have implemented an \exit link on my site. When it is browsed, the following function is called: