Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. simplexml_load_string returning empty structure, no errors. why?

simplexml_load_string returning empty structure, no errors. why?

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
xmlhelpdatabasecomjson
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GregStevens
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • G GregStevens

      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

      G Offline
      G Offline
      GregStevens
      wrote on last edited by
      #2

      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!

      P 1 Reply Last reply
      0
      • G GregStevens

        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!

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        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.

        G 1 Reply Last reply
        0
        • P Peter_in_2780

          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.

          G Offline
          G Offline
          GregStevens
          wrote on last edited by
          #4

          Thanks for the suggestion! I've done it, and the article is now posted. Cheers!

          --Greg

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups