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.
What you want to do, you can't write in PHP as an expression:
where
$sqlrow['childnodename'] = "classification->pn"(as stored in your mysql table).What you would need to write is:
(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