Reference SimpleXML node name via variable

43 Views Asked by At

need to parse a large XML document and add the values into a SQL db. I'd like to store all of the key/value pairs in a sql db to simplify my PHP code. Not all of the nodes are present in all of the XML records, so want to iterate through the DB values, i.e. classification->pn, then see if the node exists, then add the value to the SQL insert. Can't get it to work:

So instead of:

foreach($xmldata->children() as $node) {  
  if(isset($node->classification->pn)) {
     ...
  }
}

I want to do:

foreach($xmldata->children() as $node) {  
  if(isset($node->$sqlrow['childnodename'])) {
     ...
  }
}

where $sqlrow['childnodename'] = "classification->pn" as stored in my sql table.

Tried treating it like a dynamic variable, using $$childnodename, etc., but nothing seems to work.

1

There are 1 best solutions below

0
hakre On

What you want to do, you can't write in PHP as an expression:

foreach($xmldata->children() as $node) {  
  if(isset($node->$sqlrow['childnodename'])) {
     ...
  }
}

where $sqlrow['childnodename'] = "classification->pn" (as stored in your mysql table).

What you would need to write is:

$parts = explode('->', $sqlrow['childnodename']);
foreach($xmldata->children() as $node) {
  $cursor = $node;
  foreach($parts as $part) {
    if (!$cursor->$part) {
        continue 2; # skip to next ->children() $node
    }
    $cursor = $cursor->$part;
  }
  // $cursor is here the descendant (children, grandchildren, etc.)  you're looking for
}

(exemplary only)

This is because "->" within "classification->pn" is taken as part of the XML elment-name in SimpleXMLElement which is invalid as the > character is never allowed for XML element names.

You need to handle the traversal more verbosely as otherwise SimpleXMLElement can only refuse your request, simply because it is not an XML element name.

Cf. Allowed symbols in XML element name