Jump to content

simplexml problem


robin18

Recommended Posts

Hello,

 

I'm trying to read the data of a xml file with the help of simplexml.  I'm having trouble reading subchilds.

 

For example im trying to read the following xml:

 

<root>

    <child>

          <subchild>

                  <dataineed/>

                  <dataineed/>

                  <dtaineed/>

          <subchild>

    </child>

</root>

 

I'm using the following code:

 

<?php

 

$oLinkjes = new SimpleXMLElement('http://feedlink.php', 0, TRUE);

 

$countcat = count($oLinkjes->subchild);

 

echo $countcat;

 

?>

 

It echo's the the result '0' which means that it does not successfully reads the subchild. Because of that i can't get the data i want.

When i change subchild to child is does echo '1'.

 

Anyone knows what I'm doing wrong here? I'm new to xml so there might be a easy fix to this.

Link to comment
https://forums.phpfreaks.com/topic/248536-simplexml-problem/
Share on other sites

Your XML (if that is your actual document) is poorly structured, you are missing the closing tag on the <child> element.

SimpleXMLElement will throw errors and give a bad result.

Also, you are also incorrectly traversing the XML

$countcat = count($oLinkjes->child->subchild);

will return 1 as there is 1 <subchild> under <child> under <root>.

Link to comment
https://forums.phpfreaks.com/topic/248536-simplexml-problem/#findComment-1276365
Share on other sites

Sorry for the double post but i got 1 last question.

 

When u have the following .xml

 

<people>

<data>

<name>

<item1>data</item1>

<item2>data</item2>

<item3>data</item3>

</name>

<something>

<some><data></some>

</something>

<name>

<item1>dataineed</item1>

<item2>dataineed</item2>

</name>

</data>

</people>

 

I am interested in getting the data from the second child called 'name'.

I can get the data from the first child name, but i want the data from the second child called name.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/248536-simplexml-problem/#findComment-1276453
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.