Im new to XML and decided I would try and use it to implement a simple CMS compared to constructing a whole MySQL database. However Im stuck on how to print things out on the webpage. In all tutorials I’ve seen they loop through each XML element and print out every one, I only want to print out the one where page name = home or page name = mypage.
This is simply done in MySQL with WHERE page[“home”], but I cant find what the same code using XML is.
<?xml version="1.0" encoding="utf-8"?>
<content>
<page name = "home"><br />
<title>Home</title><br />
<subtitle>a test of my testing</subtitle><br />
<top-papa>blah blah top para</top-papa><br />
<mid-para>blah blah mid para</mid-para><br />
<bot-para>blah blah bot para</bot-para><br />
<image>
<imge>mememem</imge><br />
</image>
</page>
<page name = "whatdowedo"><br />
<title>What do we do?</title><br />
<subtitle>a test of my testing</subtitle><br />
<top-papa>blah blah top para</top-papa><br />
<mid-para>blah blah mid para</mid-para><br />
<bot-para>blah blah bot para</bot-para><br />
<image>
<imge>mememem</imge><br />
</image>
</page>
</content>
Many thanks to all replies.
Ben
You could use XPath queries on your XML document.
For example, if you have your XML in a string :
And load it using
simplexml_load_strin():You can then use the
SimpleXMLElement::xpath()method to do an XPath query on that XML document — searching, for example, by value of thenameattribute :And, in this specific case, you’d get the following output :
Note : the same kind of thing is, of course, possible with
DOMDocument, andDOMXpath.