Re: [請益] 請問DOM中的節點列是指??

看板PHP作者 (小喵村民(金5肉10))時間19年前 (2006/11/15 22:25), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《jimshsu (jims)》之銘言: : <tgroup cols="4"> : <thead> : <row> : <entry>Title</entry> : <entry>Author</entry> : <entry>Language</entry> : <entry>ISBN</entry> : <entry>2006</entry> : </row> : </thead> : <tbody> : <row>.........略.... : 以上是我測試的資料片段 : 我用php得dom..先取得<row>節點列 : $row = $doc->getElementsByTagName('row'); : echo $row->item(0)->nodeValue."\n"; : 若列印第一個row節點會有以下結果 : *************** : Title : Author : Language : ISBN : 2006 : ***************** : 因為我想是雁如何個別取到<entry>...我用了childNodes方法 : $list = $row->item(0)->childNodes; : 結果卻得到 : $list->length=11 : 列印的的話--echo "Value=".$list->item($i)->nodeValue."\n"; : Value= : Value=Title : Value= : Value=Author : Value= : Value=Language : Value= : Value=ISBN : Value= : Value=2006 : Value= : 疑問: : 1.請問那個長度11是怎麼算的呢? : 這長度不是指該節點的元素個數? 會不會把下面別的 entry 也算進去了? : 2.回到我最初的好奇..若我取得所有row元素的節點 : 我要用何種方式取得<entry>的nodeValue? 小弟習慣結合 domxPath 去做,免得 parent , child 跑來跑去弄亂了 給您參考一下做法 <?php $testXML = "<thead> <row> <entry>Title</entry> <entry>Author</entry> <entry>Language</entry> <entry>ISBN</entry> <entry>2006</entry> </row> </thead>"; $xml_config = new DOMDocument(); $xml_config->loadXML($testXML); $xpath = new domXPath($xml_config); $dom_query = "//thead/row/*"; $xpathQuery = $xpath->query($dom_query); $size = $xpathQuery->length; for ($i=0; $i<$size; $i++){ $node = $xpathQuery->item($i); echo $node->nodeValue; } ?> -- 修電腦條款(來自一個好人的怒吼) http://0rz.tw/ae1rm -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.232.140.201
文章代碼(AID): #15MoC-9t (PHP)
文章代碼(AID): #15MoC-9t (PHP)