-1

i have this string in php, this string i think is already well formatted like an xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Books>
<ID>1</ID>
<Title>Programming 1</Title>
<ISBN>1234567890001</ISBN>
<Author>Kevin</Author>
<Abstract>This Book is about programming</Abstract>
</Books>


So i convert this string into xml using simplexml_load_string() function, but the thing is it never resulted back as an xml so every time i do the print_r(), i will got like this:

SimpleXMLElement Object ( )

i think it should be contain all the tree and nodes as usual, but the object returned is always null. i already tried several other method. But all of them will give the same result which is blank XML object.
So is there something wrong with my string format? or maybe there's something wrong with my code. here is my code:

<?php

$ISBN=$_POST["ISBN"];
$url='http://localhost:8080/Programming/services/hello_world/DatabaseSearch?ISBN='.$ISBN;
$xmlstr = file_get_contents($url);
echo $xmlstr;
$xml=simplexml_load_string($xmlstr);
echo $xml->Books->Title;
print_r($xml);
?>

the

echo $xml->Books->Title;

return only null value. So i don't know what's actually wrong, i already trying to fix this for days now but no result. Please help me..

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
KevinL
  • 83
  • 2
  • 10
  • You are just confusing the output with `NULL`, it's just not the case that the output is `NULL`. Please use the search, this has been discuessed and Q&A'ed quite some time on this website already. – hakre Oct 26 '13 at 21:55
  • possible duplicate of [SimpleXML and print\_r() - why is this empty?](http://stackoverflow.com/questions/3109302/simplexml-and-print-r-why-is-this-empty) – hakre Oct 26 '13 at 21:55
  • 1
    Actually, the problem here is that the `Books` element *is* `$xml`, so you don't need `$xml->Books`, just `$xml->Title`. Also a common problem, but I don't have a good reference to hand to mark as duplicate of. – IMSoP Oct 27 '13 at 19:35
  • No it's my fault. my string contain namespace part. this part actually not printed into echo, so the xml_load always faile because it's not find the but i solved this problem by substr. – KevinL Oct 28 '13 at 05:52

1 Answers1

0

You can get the title by echo $xml->Title;

Actually $xml=simplexml_load_string($xmlstr); return the root node

also you have no other node in xml so you can get the title echo $xml->Title;

for more details see SimpleXML and print_r() - why is this empty?

Community
  • 1
  • 1
Harish Singh
  • 3,359
  • 5
  • 24
  • 39
  • I tried it, but it's not working. i think just by looking at blank print_r() result then it's mean simplexml_load_string() is failed convert my string into xml. – KevinL Oct 25 '13 at 17:35
  • are you getting xml string with file_get_contents($url), if not then your $url may be incorrect or you have no permission of file_get_contents( please check allow_url_fopen in php.ini ) – Harish Singh Oct 26 '13 at 03:33
  • For duplicate questions please just leave a link to the duplicate question. Thanks. – hakre Oct 26 '13 at 21:56