1

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 ?

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
user3201764
  • 25
  • 1
  • 1
  • 4
  • Please post a working code to get an answer here. You need 'How can retrieve id of node next to in php ?' but there are other issues with this code. However, here is the solution, you can run the $movies in a `foreach` and set if condition for 'id' and can get data. – Neocortex Mar 14 '14 at 06:18
  • No there is a false understanding. Working code is different finding a solution to develop a code is different. You are wrapping up one or more questions and bugs by asking one question. You need answer for number 5 on scale of 10 but u didn't solve 1,2,3 and 4 this is wat i try to mean. Hope you didn't try the `foreach` with `if` condition. – Neocortex Mar 14 '14 at 06:31
  • Don't know xform xml parsing. I tried sample xml and get the id of node like that method. But in this its not working. – user3201764 Mar 14 '14 at 06:38
  • What you mean by one or more questions and bugs? – user3201764 Mar 14 '14 at 06:39

2 Answers2

1

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";
                }
            }
        }
    }
}
?>
Neocortex
  • 653
  • 9
  • 32
0

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>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95