Let’s say you have a xml file which looks like:
<keywords>
<keyword>
<title>Business Blogging</title>
<date>2009-12-31</date>
</keyword>
<keyword>
<title>Insurance Quotes</title>
</keyword>
</keywords>And you want apply some logic based on whether the xml node keywords->keyword->date exist. So how would you do it?
Let’s say that the xml file is saved as keywords.xml
1. Load the xml using simplexml
$xml = simplexml_load_file("keywords.xml");2. Loop and check if the date node exist
foreach ($xml->keywords->keyword as $keyword) {
if (isset($keyword->date)) {
// date node is found. do whatever you want to do here.
}
}I haven’t check but there should also be a way to check whether a node exists without involving a foreach loop using simplexml xpath’s function. Please leave a comment if you know a better way.
Zeeshan Dar says
Use
if isObject(objLst.item(i).childNodes(0)) then
end if
David says
@zeeshan
Is that using php dom extension coz I sense that there’s something wrong with the syntax…?
I would personally prefer simplexml over the dom extension since it’s easier to learn and understand.