Ok guys I’m using xml to store page information for a dynamic website. I need to figure out a way to take a variable(file name) and pull the related node and parse that information. so basically here’s my XML structure…
<SITE>
<PAGE>
<FILENAME>sub.php</FILENAME>
<DESCRIPTION>this is an example sub page</DESCRIPTION>
<TITLE>NOH Sub page</TITLE>
<PARENTS>
<PARENT>What is NOH</PARENT>
</PARENTS>
</PAGE>
<PAGE>
<FILENAME>about.php</FILENAME>
<DESCRIPTION>description description description </DESCRIPTION>
<TITLE>About Us</TITLE>
<PARENTS>
<PARENT>Company</PARENT>
<PARENT>People</PARENT>
</PARENTS>
</PAGE>
</SITE>
Is there a way to use SimpleXML and PHP to take a variable say.. ‘sub.php’ and say I want the node where filename = ‘sub.php’, and then be able to parse that node(page) for needed info. Thanks!
Update, where I’m confused is…
i have this function
function getPage($pagePath){
$file = "includes/sitestructure.xml";
$xml = simplexml_load_file($file);
return print_r($xml-xpath(PAGE/FILENAME));
}
but my xpath doesn’t work, and I’m not sure how to logically get the info I need.. I know how to normally parse XML with PHP but not how to specify a specific node by comparing it to a variable then parse it.
This is broken in several ways, but this is an XPath question not a PHP syntax one.
You want to get the
PAGEelement(s) which contain aFILENAMEof your chosen file name. To do that in XPath, a predicate can be used.In your PHP code that might look like
The important point here is the use of the predicate to filter the result to only the page element(s) that you want.