0

I wrote following php code to extract nodes information from this xml:

<sioctBoardPost rdfabout="http//boards.ie/vbulletin/showpost.php?p=67075">
  <rdftype rdfresource="http//rdfs.org/sioc/ns#Post" />
  <dctitle>hib team</dctitle>
  <siochas_creator>
    <siocUser rdfabout="http//boards.ie/vbulletin/member.php?u=497#user">
      <rdfsseeAlso rdfresource="http//boards.ie/vbulletin/sioc.php?sioc_type=user&amp;sioc_id=497" />
    </siocUser>
  </siochas_creator>
  <dctermscreated>1998-04-25T213200Z</dctermscreated>
  <sioccontent>zero, those players that are trialing 300 -400 pingers? umm..mager lagg and even worse/</sioccontent>
</sioctBoardPost>

<?php
$xml = simplexml_load_file("boards.xml");
$products[0] = $xml->xpath("/sioctBoardPost/sioccontent");
$products[1] = $xml->xpath("/sioctBoardPost/dctermscreated");
$products[2] = $xml->xpath("/sioctBoardPost/@rdfabout");
print_r($products);
  ?>

This gives following output:

Array ( 
[0] => Array ( [0] => SimpleXMLElement Object ( [0] => zero, those players that are trialing for hib team, (hpb's) most of them are like 300 -400 pingers? umm..mager lagg and even worse when they play on uk server's i bet/ ) ) [1] => Array ( [0] => SimpleXMLElement Object ( [0] => 1998-04-25T213200Z ) ) [2] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [rdfabout] => http//boards.ie/vbulletin/showpost.php?p=67075 ) ) ) 
) 

But I need only nodes content as an output i.e without Array([0] => Array etc.

Output should be like this:

zero, those players that are trialing for hib team, (hpb's) most of them are like 300 -400 pingers? umm..mager lagg and even worse when they play on uk server's i bet

1998-04-25T213200Z

http//boards.ie/vbulletin/showpost.php?p=67075

Thanks in advance

2
  • Could you show your xml file? Commented Jan 17, 2013 at 5:51
  • <sioctBoardPost rdfabout="http//boards.ie/vbulletin/showpost.php?p=67075"> <rdftype rdfresource="http//rdfs.org/sioc/ns#Post" /> <dctitle>hib team</dctitle> <siochas_creator> <siocUser rdfabout="http//boards.ie/vbulletin/member.php?u=497#user"> <rdfsseeAlso rdfresource="http//boards.ie/vbulletin/sioc.php?sioc_type=user&amp;sioc_id=497"/> </siocUser> </siochas_creator> <dctermscreated>1998-04-25T213200Z</dctermscreated> <sioccontent>zero, those players that are trialing 300 -400 pingers? umm..mager lagg and even worse/</sioccontent> </sioctBoardPost> Commented Jan 17, 2013 at 8:02

3 Answers 3

1

You can use current() to only get the first element of each XPath result (which is an array) and then use a (string) cast to get the node contents:

$products[0] = (string)current($xml->xpath("/sioctBoardPost/sioccontent"));
$products[1] = (string)current($xml->xpath("/sioctBoardPost/dctermscreated"));
$products[2] = (string)current($xml->xpath("/sioctBoardPost/@rdfabout"));
print_r($products);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for suggestion and you code return following output: Array ( [0] => SimpleXMLElement Object ( [0] => hib team ) [1] => SimpleXMLElement Object ( [0] => zero, those players that are trialing for hib team, (hpb's) most of them are like 300 -400 pingers? umm..mager lagg and even worse when they play on uk server's i bet/ ) [2] => SimpleXMLElement Object ( [0] => 1998-04-25T213200Z ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [rdfabout] => http//boards.ie/vbulletin/showpost.php?p=67075 ) ) ) but I dont want the word SimpleXMLElement Object in output.
@MuhammadShahzadFaisal You may need an additional (string) cast.
Actually I dont know how to apply (string) cast on this, kindly help me how to do that.
@MuhammadShahzadFaisal I've shown you in my updated answer, please check.
@MuhammadShahzadFaisal Cool, if all is well, please consider to accept this answer by clicking the checkmark next to it. Thanks!
0

As you have observed, the xpath() method returns an array of matched nodes, so you need to deal with the elements of the returned arrays. I believe this should work in this case:

$xml = simplexml_load_file("boards.xml");
$products[0] = $xml->xpath("/sioctBoardPost/sioccontent")[0];
$products[1] = $xml->xpath("/sioctBoardPost/dctermscreated")[0];
$products[2] = $xml->xpath("/sioctBoardPost/@rdfabout")[0];
print_r($products);

3 Comments

But I have 5.3.5 and above code give following error: Parse error: syntax error, unexpected '[' in C:\wamp\www\temptry.php on line 10
Could you provide your XML above in your question?
<sioctBoardPost rdfabout="http//boards.ie/vbulletin/showpost.php?p=67075"> <rdftype rdfresource="http//rdfs.org/sioc/ns#Post" /> <dctitle>hib team</dctitle> <siochas_creator> <siocUser rdfabout="http//boards.ie/vbulletin/member.php?u=497#user"> <rdfsseeAlso rdfresource="http//boards.ie/vbulletin/sioc.php?sioc_type=user&amp;sioc_id=497"/‌​> </siocUser> </siochas_creator> <dctermscreated>1998-04-25T213200Z</dctermscreated> <sioccontent>zero, those players that are trialing 300 -400 pingers? umm..mager lagg and even worse/</sioccontent> </sioctBoardPost>
0

This should get you what you need...

foreach ($products as $product) { // iterate through the $products array
    print $product[0]->nodeValue  // output the node value of the SimpleXMLElement Object
}

1 Comment

Thank you for suggestion but it does not show any thing as output, a blank screen appears

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.