开发者

XPath result to PHP Object

I have an xml file which represents a number of objects for which I have a class in my application. For example, blogposts:

<blogposts>
  <blogpost id="604">
    <title>afdghadfh</title>
    <body>adfgadfgda</body>
  </blogpost>
  <blogpost id="605">
    <title>dafghadh</title>
    <body>afadf</body>
  </blogpost>
</blogposts> 

I would like to read the xml file using X开发者_如何学GoPath and convert the results to blogpost objects. Is there any simple way to convert the resulting SimpleXMLElement Objects into values for a blogpost object?

Any advice appreciated.

Thanks.


Tweak as needed.

// blogpost class definition

$blogposts = array();

$xml_resource = new SimpleXMLElement('file.xml', 0, true);

foreach($xml_resource->xpath('/blogposts/blogpost') as $blogpost)
{
    $current_blogpost = new blogpost();
    $current_blogpost->id = (int) $blogpost['id'];
    $current_blogpost->title = (string) $blogpost->title;
    $current_blogpost->body = (string) $blogpost->body;
    $blogposts[] = $current_blogpost;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜