0

Recently I got from here about how to parse large xml files using of XMLReader and SimpleXML in PHP. I tried to adapt the code of above mentioned tutorial into my php procedure like this:

$xml_url = "http://localhost/rest/server.php?wstoken=".$token&function=contents";
    $reader = new XMLReader;
    $reader->open($xml_url);

    while($reader->read()){
        if($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'SINGLE'){
            $doc = new DOMDocument('1.0','UTF-8');
            $xml = simplexml_import_dom($doc->importNode($reader->expand(), true));
            //$titleString = (string) $xml->description;
            echo $xml->description;
        }
    }

The XML file called via url is so (the xml version is here): screenshot

Other SINGLE tags (marked with 'red') have the same structure and I want to print 'description' of them also.

The output is with above mentioned php procedure is: error on line 1 at column 1: Extra content at the end of the document. Any help would be great.

1
  • Can't you simply use simplexml_load_file and xpath? Commented Jan 23, 2013 at 3:35

1 Answer 1

1

SimpleXML function should be enough:

$xml=simplexml_load_file('http://dl.dropbox.com/u/72519118/response.xml');
var_dump($xml->xpath('//SINGLE/KEY[@name="description"]/VALUE/text()'));

The above var_dump outputs:

array(3) {
  [0]=>
  object(SimpleXMLElement)#2 (1) {
    [0]=>
    string(1703) "<div class="no-overflow">..."
  }
  [1]=>
  object(SimpleXMLElement)#3 (1) {
    [0]=>
    string(9906) "<div class="no-overflow">..."
  }
  [2]=>
  object(SimpleXMLElement)#4 (1) {
    [0]=>
    string(4114) "<div class="no-overflow">..."
  }
}

Note that tag names in xpath() is case sensitive, so '//single/key...' doesn't work.

Addition:

The "standard" way to retrieve text value in SimpleXML is $KEY->VALUE;

However if you have reached the "end" node of the XML tree (like I do in my XPath), you can simply type-cast it to string to get the value:

$xml=simplexml_load_file('http://dl.dropbox.com/u/72519118/response.xml');
$result=$xml->xpath('//SINGLE/KEY[@name="description"]/VALUE/text()');
foreach($result as $text)
{
    var_dump((string)$text);
}

The above outputs:

string(1703) "<div class="no-overflow"><p>..."
string(9906) "<div class="no-overflow"><h3>..."
string(4114) "<div class="no-overflow"><h3>..."
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your reply, i used it, but the result is now : error on line 1 at column 1: Document is empty
@Dozent I tried this code again and still get the same result. Are you sure your server.php generates the same valid XML as you post on DropBox?
yes now it's works, i was mistakenly put Content-type: application/xml rather than text/html. The result is now: array(15) { [0]=> object(SimpleXMLElement)#2 (0) { } [1]=> object(SimpleXMLElement)#3 (0) { } [2]=> object(SimpleXMLElement)#4 (0) { ... to be honest I don't know how to extract values from this set of arrays. Can you show me the approach? Thanks

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.