开发者

How can I reconstruct a paginated XML document?

I'm reading from an API that returns paginated XML. The basic format of the API's output is:

<candidates.list page="1" next_page="yes">
  <candidate />
  <candidate />
  <candidate />
</candidates.list>

My code is like this:

while (TRUE) {
  $xml_str = file_get_contents($dest)
  $xml = new SimpleXMLElement($xml_str);

  // What should I do to append subsequent pages to my first page's XML?

  if 开发者_如何学Python( $xml['next_page'] == 'yes' ) {
    $dest = $new_destination;  // I figure out the next page's API query here
  }
  else {
    break;
  }
}

return $xml;

Happy 4th of July, and thank you!!


I would append the next page's candidates into your $xml. You have already parsed the XML into a SimpleXMLElement. Or build your own array of SimpleXMLElement candidate objects if that's all you care about.

As a side note, while(1) is bad form. You could change your logic or use a do/while() loop.

Happy 4th back!


SimpleXML was the wrong tool for the job. SimpleXML is not designed for adding new nodes, or doing any kind of manipulation really. I switched to using DOMDocument, and was quickly able to create a solution using the appendChild function.


Yes, you have to use DOM-based solutions or build a different data structure on the fly with (for example) a SAX approach. DOM doesn't scale well for high throughput (the object memory footprint is very heavy), so if this is the only manipulation you are doing, you might want to consider SAX if you need to scale up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜