pahunrepublic Posted June 1, 2011 Share Posted June 1, 2011 Hi everyone! I'm making friends with DOM+PHP+XML. I found an example in w3school tutorial but there are some things I don't understand. <?php $q=$_GET["q"]; $xmlDoc = new DOMDocument(); $xmlDoc->load("cd_catalog.xml"); $x=$xmlDoc->getElementsByTagName('ARTIST'); for ($i=0; $i<=$x->length-1; $i++) { //Process only element nodes if ($x->item($i)->nodeType==1) { if ($x->item($i)->childNodes->item(0)->nodeValue == $q) { $y=($x->item($i)->parentNode); } } } $cd=($y->childNodes); for ($i=0;$i<$cd->length;$i++) { //Process only element nodes if ($cd->item($i)->nodeType==1) { echo("<b>" . $cd->item($i)->nodeName . ":</b> "); echo($cd->item($i)->childNodes->item(0)->nodeValue); echo("<br />"); } } ?> This part I understand, it's a PHP method: $xmlDoc = new DOMDocument(); $xmlDoc->load("cd_catalog.xml"); $x=$xmlDoc->getElementsByTagName('ARTIST'); but I don't get the rest , and what is this $x->length-1 means? What does this mean? nodeType==1 what do the numbers represent? Can anyone give me the URL of a good XML PHP tutorial? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/238047-can-someone-explain-this-phpxml-code-to-me/ Share on other sites More sharing options...
jcbones Posted June 1, 2011 Share Posted June 1, 2011 $x->length-1 //subtract 1 from the $x->length, which is the amount of nodes in your object. Node types are: NodeType Named Constant 1 ELEMENT_NODE 2 ATTRIBUTE_NODE 3 TEXT_NODE 4 CDATA_SECTION_NODE 5 ENTITY_REFERENCE_NODE 6 ENTITY_NODE 7 PROCESSING_INSTRUCTION_NODE 8 COMMENT_NODE 9 DOCUMENT_NODE 10 DOCUMENT_TYPE_NODE 11 DOCUMENT_FRAGMENT_NODE 12 NOTATION_NODE Quote Link to comment https://forums.phpfreaks.com/topic/238047-can-someone-explain-this-phpxml-code-to-me/#findComment-1223273 Share on other sites More sharing options...
pahunrepublic Posted June 1, 2011 Author Share Posted June 1, 2011 But why subtract 1? Quote Link to comment https://forums.phpfreaks.com/topic/238047-can-someone-explain-this-phpxml-code-to-me/#findComment-1223275 Share on other sites More sharing options...
jcbones Posted June 1, 2011 Share Posted June 1, 2011 Because arrays start at index 0, so if the array count is 15, your highest index will be 14. Quote Link to comment https://forums.phpfreaks.com/topic/238047-can-someone-explain-this-phpxml-code-to-me/#findComment-1223286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.