Xform xml parsing

528 views Asked by At

In the xform xml i want to parse xml and access the id of node. It's the xform xml and i want to access the id of node test_geopoint (id=test_geopoint). But the node name will change for each xform xml.

    <?php
$xmlstr = <<<XML <?xml version="1.0"?>
    <h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <h:head>
        <h:title>test_geopoint</h:title>
        <model>
          <instance>
            <test_geopoint id="test_geopoint">
              <name/>
              <geopoint/>
              <meta>
                <instanceID/>
              </meta>
            </test_geopoint>
          </instance>

      </h:body>
    </h:html> $XML;

i tried the code like this, but not access the id of node after the <instance>.

$movies = new SimpleXMLElement($xmlstr);
echo $movies->model->instance->children()[0]['id'];
and
echo $movies->head->title->model->instance->children()[0]['id'];

How can retrieve id of node next to <instance> in php ?

2

There are 2 answers

1
Neocortex On

The below example is the same as you expecting, use it the same way I have accessed, the name in a array. You are suppose to mention as instanceId

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#211;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;

$movies = new SimpleXMLElement($xmlstr);


echo '<pre>';

foreach($movies as $moviesdata){
    foreach($moviesdata as $moviesdatavalues){
        foreach($moviesdatavalues as $mdvk=>$mdn){
            foreach($mdn as $c=>$v){
                    if($c == 'name'){
                        echo $v."\n";
                }
            }
        }
    }
}
?>
1
Daniel Haley On

The default namespace is http://www.w3.org/2002/xforms so every unprefixed element descendant of html is in that namespace; including your instance data.

Try adding an empty namespace (xmlns="") to your instance data...

<model>
    <instance>
        <test_geopoint xmlns="" id="test_geopoint">
            <name/>
            <geopoint/>
            <meta>
                <instanceID/>
            </meta>
        </test_geopoint>
    </instance>
    <bind nodeset="/test_geopoint/name" type="string"/>
    <bind nodeset="/test_geopoint/geopoint" type="geopoint"/>
    <bind calculate="concat('uuid:', uuid())" nodeset="/test_geopoint/meta/instanceID" readonly="true()" type="string"/>
</model>