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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.