simplexml_load_string returning empty structure, no errors. why?
-
I have a string variable
$xml
which contains this string:<rdf:rdf xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:sumo="http://www.ontologyportal.org/translations/SUMO.owl.txt#" xmlns:dul="http://www.loa-cnr.it/ontologies/DUL.owl" xmlns:owl="http://www.w3.org/2002/07/owl" xmlns:top="http://talkingowlproject.com/schemas/top-level-concepts-10/" xmlns:wps="http://talkingowlproject.com/schemas/wordnet-parser-schema-10/"><rdf:Description rdf:id="#me">
rdfs:labelPigwidgeon</rdfs:label>
<rdf:type rdf:resource="http://talkingowlproject.com/schemas/top-level-concepts-10/TalkingOwl" />
</rdf:Description>
<rdf:Description rdf:id="#you">
rdfs:labelgreg</rdfs:label>
<rdf:type rdf:resource="http://talkingowlproject.com/schemas/top-level-concepts-10/User" />
</rdf:Description></rdf:rdf>I have the following code:
$xmlobj = simplexml_load_string($xml);
if ($xmlobj===false) die('bad news');
print_r($xmlobj);The result displayed is (appears to be?) an empty structure:
SimpleXMLElement Object
(
)And when I try to iterate over members of the object, it performs no iterations (confirming that the structure is empty). Is there something wrong with the XML string? Is there a problem because of the namespace declarations or the use of namespaces on every tag? When I remove the namespace declarations in the root tag, I actually get a structure with contents .... but then it doesn't know the namespaces, so it can't use them when I'm parsing the object. (I need to be able to identify namespaces with nodes.) Thank you for your help. --Greg
-
I have a string variable
$xml
which contains this string:<rdf:rdf xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:sumo="http://www.ontologyportal.org/translations/SUMO.owl.txt#" xmlns:dul="http://www.loa-cnr.it/ontologies/DUL.owl" xmlns:owl="http://www.w3.org/2002/07/owl" xmlns:top="http://talkingowlproject.com/schemas/top-level-concepts-10/" xmlns:wps="http://talkingowlproject.com/schemas/wordnet-parser-schema-10/"><rdf:Description rdf:id="#me">
rdfs:labelPigwidgeon</rdfs:label>
<rdf:type rdf:resource="http://talkingowlproject.com/schemas/top-level-concepts-10/TalkingOwl" />
</rdf:Description>
<rdf:Description rdf:id="#you">
rdfs:labelgreg</rdfs:label>
<rdf:type rdf:resource="http://talkingowlproject.com/schemas/top-level-concepts-10/User" />
</rdf:Description></rdf:rdf>I have the following code:
$xmlobj = simplexml_load_string($xml);
if ($xmlobj===false) die('bad news');
print_r($xmlobj);The result displayed is (appears to be?) an empty structure:
SimpleXMLElement Object
(
)And when I try to iterate over members of the object, it performs no iterations (confirming that the structure is empty). Is there something wrong with the XML string? Is there a problem because of the namespace declarations or the use of namespaces on every tag? When I remove the namespace declarations in the root tag, I actually get a structure with contents .... but then it doesn't know the namespaces, so it can't use them when I'm parsing the object. (I need to be able to identify namespaces with nodes.) Thank you for your help. --Greg
I finally found out what was wrong, and came up with a solution! I hope it isn't considered "bad form" to link to a blog post, but I wrote a long and involved blog explaining my frustration and what I found out the problem to be here: http://talkingowlproject.blogspot.com/2011/06/simplexml-and-namespace-quirks.html[^] And I actually wrote my own class,
SimpleRDFElement
, to extend the SimpleXMLElement class and solve the problems. You can read my description of my extension class and download the 1-page source code here: http://talkingowlproject.blogspot.com/2011/06/simplerdfelement-class-extension-of.html[^] In a nut shell: the built in functions with SimpleXML handle namespaces poorly, the children() and attributes() methods only allow you to select children and attributes in a particular namespace (not all at once), and there are no functions specifically for extracting the namespace portion of the tag of the current top-level element. So, my extension class, SimpleRDFElement, provides functions to solve all of these problems, as well as a method for parsing an RDF XML string into triples. I hope that this helps anyone else who is interested in this question! -
I finally found out what was wrong, and came up with a solution! I hope it isn't considered "bad form" to link to a blog post, but I wrote a long and involved blog explaining my frustration and what I found out the problem to be here: http://talkingowlproject.blogspot.com/2011/06/simplexml-and-namespace-quirks.html[^] And I actually wrote my own class,
SimpleRDFElement
, to extend the SimpleXMLElement class and solve the problems. You can read my description of my extension class and download the 1-page source code here: http://talkingowlproject.blogspot.com/2011/06/simplerdfelement-class-extension-of.html[^] In a nut shell: the built in functions with SimpleXML handle namespaces poorly, the children() and attributes() methods only allow you to select children and attributes in a particular namespace (not all at once), and there are no functions specifically for extracting the namespace portion of the tag of the current top-level element. So, my extension class, SimpleRDFElement, provides functions to solve all of these problems, as well as a method for parsing an RDF XML string into triples. I hope that this helps anyone else who is interested in this question!Good one! Would you consider massaging your writeup into a tip/trick or article for CP? It's exactly the right kind of stuff. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.
-
Good one! Would you consider massaging your writeup into a tip/trick or article for CP? It's exactly the right kind of stuff. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.
Thanks for the suggestion! I've done it, and the article is now posted. Cheers!
--Greg