PDA

View Full Version : query xml file


azwani
10-09-03, 09:54 AM
hi, can anyone help me to query this xml doc using XPath
i want to retrieve record --> first name=ana, street=jintan

<record>
<name>
<first>ana</first>
<last>john</last>
</name>
<address>
<street>betawi</street>
<postcode>27650</postcode>
</address>
<name>
<first>ana</first>
<last>smith</last>
</name>
<address>
<street>jintan</street>
<postcode>17000</postcode>
</address>
</record>

v1brazy
12-17-05, 08:38 AM
function checkName($name){

// Create a DOMDocument instance
$xml = new DomDocument();
$xml->preserveWhiteSpace = false;
$xml->load("xmlfile.xml");
$xpath = new domxpath($xml);

$qry = "//name[name='$name']/street";

$exec_query = $xpath->query($qry);

foreach ($exec_query as $entry) {

echo "{$entry->nodeValue}";//name
echo "{$entry->nextSibling->nodeValue}";//street

}

}
checkName("ana");

Hopefully this helps