Jump to content

XML - Listing Children Nodes


Scrutters

Recommended Posts

Hi,

 

I need something list all the nodes of an XML file in a similar way to how the print_r() function lists out an array.

So it will list out all the fields in the correct order but so that I can actually do something with them.

 

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
    [d] => Array
        (
            [0] => x
            [1] => y
            [2] => Array
                 (
                     [0] => Keeps going deeper...
                     [1] => etc...
                 )
        )
)

 

I'm trying to create a select box with all the fields in so that I can map the XML file into my database.

At the moment I am doing this:

 

foreach($xml->children() as $child1){
        foreach($child1->children() as $child2){
                 foreach($child2->children() as $child3){
                         // Obviously this could keep going for ages...
                 }
        }
}

 

Really need something that can test to see if there are any children nodes under the current node selected and keep going till it stops then move on the the next parent node.

Or something that can count all nodes within the feed and just list them one by one.

 

I'm sure there must be a way to do this, I just can't work out the logic.

Link to comment
Share on other sites

The code he is using is copied from a website it is as follows but it does not call the child nodes.

 

$file = "disc.xml";
$xml_parser = xml_parser_create();

if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

$data = fread($fp, filesize($file));
fclose($fp);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

$params = array();
$level = array();
foreach ($vals as $xml_elem) {
  if ($xml_elem['type'] == 'open') {
    if (array_key_exists('attributes',$xml_elem)) {
      list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
    } else {
      $level[$xml_elem['level']] = $xml_elem['tag'];
    }
  }
  if ($xml_elem['type'] == 'complete') {
    $start_level = 1;
    $php_stmt = '$params';
    while($start_level < $xml_elem['level']) {
      $php_stmt .= '[$level['.$start_level.']]';
      $start_level++;
    }
    $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
    eval($php_stmt);
  }
}

echo "<pre>";
print_r ($params);
echo "</pre>";

Link to comment
Share on other sites

Ideally the select box would end up something like this...

 

<select name="test">
<optgroup label="Parent">
<option value="etc1">etc1</option>
<option value="etc2">etc2</option>
<optgroup label="etc3">
<option value="abc1">abc1</option>
<option value="abc3">abc2</option>
</optgroup>
</optgroup>
</select>

 

I have recieved a reply on another forum which may have answered my question, but open to other suggestions.

The suggestion I have had is to try one of the user exmaples on the PHP SimpleXML->children() function page.

Link to comment
Share on other sites

Now this is the problem, there isn't just one XML file.

There will be potentially numerous XML files in different formats.

This is why I need something to list out all available nodes within the feed.

 

Sorry a can't be any more specific, but this is what I need my system to do.

Link to comment
Share on other sites

Having example input and output would help us to see what kind of translation you're looking for given specific structures of the XML.  To clarify, there are different ways to translate the following XML:

 

<example>
  <a>apple</a>
  <a>orange</a>
  <b>
    <c>cherry</c>
    <d>damson</d>
  </b>
  <e>elderberry</e>
</example>

 

Given that (would you need to even cater for structures like that?) what would the HTML be?

Link to comment
Share on other sites

Using that example, the HTML would need to look to the following:

(Or as close as I can get it)

 

<select name="testSelect">
<optgroup label="example">
     <option>apple</option>
     <option>orange</option>
     <optgroup label="b">
          <option>cherry</option>
          <option>damson</option>
     </optgroup>
     <option>elderberry</option>
</optgroup>
</select>

 

Does that help at all?

Link to comment
Share on other sites

I've managed to get it working, but need to look into putting the XML feed into an array on page load as I reference it multiple times on a page it creating quite a load on my server.

 

I think I have the solution for that as well though.

 

Cheers for the help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.