Category ...">

How to parse XML tree using DOMDocument?

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
 <root>
     <category>
         <name>Category</name>
         <desc>Category</desc>
         <category>
             <name>Subcategory</name>
             <desc>Sub-category</desc>
             <category>
                  <name>Subcategory</name>
                  <desc>Sub-category</desc>
             </category>  
         </category>  
     </category>
 </root>

      

My tree can have as many levels as possible. There are no requirements for this.

First question: Is my XML correct for this kind of requirement? and how can i optimize it (if needed)

Second question: How can I parse it using DOMDocument?

I know how to load an XML document, but I don't know how to parse it. I read a bit about recursion, but I haven't been able to figure out correctly how to map against PHP / DOMDocument.

Thanks for the help!

EDIT

What I want to do is manage the category system. I tried with SQL, but it was too hard to manage the relational model, even with nested select, etc.

So, I want to be able to make a tree from my xml

like

  • Category
    • Subcategory
      • Subcategory

No depth limits

I want to be able to search for a category, retrieve all of its children (subcategories) (or not), its parent (s) (or not), (sisters?), Etc.

+2


source to share


4 answers


Well, there’s nothing wrong with the XML you’re using here, but you don’t say enough about what you want to do with the data for someone to give you a quality answer on whether your XML will capture what you want. As for "[parsing] with DOMDocument", you can load it into a DOMDocument object like this:

$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
 <root>
     <category>
         <name>Category</name>
         <desc>Category</desc>
         <category>
             <name>Subcategory</name>
             <desc>Sub-category</desc>
             <category>
                  <name>Subcategory</name>
                  <desc>Sub-category</desc>
             </category>  
         </category>  
     </category>
 </root>
XML;
$d = new DOMDocument();
$d->loadXML($xml);

      



At this point, the question arises again: what do you want to do about it?

+1


source


If you are just talking about how to deal with a structure like this, I would say write two functions that take the complete structure and one that takes a DOMNode category reference. The first function will do the initial processing and then pass the first reference to the initial category node. Then, in this function, you process the current node properties as needed, and then recurse to the children if present.



It would be more efficient to handle this flat, of course, in one loop, but then you will lose the literal representation of the hierarchy.

+1


source


To echo the point above about what you want to do with it ... IMHO there are three big classes of things you can do with a chunk of XML.

With the DOMDocument instantiated and the XML loaded, you can search for nodes using XPath queries, just as you search for a relational database using SQL SELECT queries. You can extract node properties, node nodes and text inside nodes. I would say what kind of parsing. The DOMDocument XPath component does this for you.

Instead, you can transform your XML into something else - a different dialect of XML, XHTML, etc. using XSL Transforms. Which may or may not be parsed as such, but requires parsing. The PHP XSLTProcessor component will do this.

Another important idea that I think DOMDocument doesn't really support is a streaming parser. The parser consumes XML in a linear fashion, while invoking callback functions on each node of interest. A somewhat venerable SAX parser is AFAIK the archetypal stream parser. PHP used to use SAX parser, I think it is now moved to PEAR or PECL.

But what do you want to do with your XML?

0


source


You said you tried SQL and it didn't work for you. Just a tip: if you are using Oracle take a look at START WITH ... CONNECT BY, if you are using SQL Server use recursive CTEs. These approaches solve the problem.

0


source







All Articles