PHP XML Strategy: Parsing DOM to fill "Bean"

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-03-30T13:06:01Z Indexed on 2010/03/30 14:33 UTC
Read the original article Hit count: 442

Filed under:
|
|
|

I have a question concerning a good strategy on how to fill a data "bean" with data inside an xml file.

The bean might look like this:

class Person
{
 var $id;
 var $forename = "";
 var $surname = "";
 var $bio = new Biography();
}

class Biography
{
    var $url = "";
    var $id;
}

the xml subtree containing the info might look like this:

<root>
 <!-- some more parent elements before node(s) of interest -->
  <person>
   <name pre="forename">
              Foo
   </name>
   <name pre="surname">
    Bar
   </name>
   <id>
    1254
   </id>
   <biography>
    <url>
     http://www.someurl.com
    </url>
    <id>
     5488
    </id>
   </biography>

  </person>
</root>

At the moment, I have one approach using DOMDocument. A method iterates over the entries and fills the bean by "remembering" the last node. I think thats not a good approach.

What I have in mind is something like preconstructing some xpath expression(s) and then iterate over the subtrees/nodeLists. Return an array containing the beans as defined above eventually.

However, it seems not to be possible reusing a subtree /DOMNode as DOMXPath constructor parameter.

Has anyone of you encountered such a problem?

© Stack Overflow or respective owner

Related posts about php

Related posts about dom