我必須在不知道節點的情況下使用XML讀取並解析XML文件。在PHP中使用XMLReader讀取XML而不知道節點
我有這個文件:
<Invoices>
<Company>
<Name>Tuttobimbi Srl</Name>
</Company>
<Documents>
<Document>
<CustomerCode>0055</CustomerCode>
<CustomerWebLogin></CustomerWebLogin>
<CustomerName>Il Puffetto</CustomerName>
</Document>
</Documents>
</Invoices>
我會分析它是這樣的:
Invoices
Invoices, Company
Invoices, Company, Name
Invoices, Documents
Invoices, Documents, Document
etc...
我寫了這個代碼:
while ($xml->read()) {
if ($xml->nodeType == XMLReader::ELEMENT)
array_push($a, $xml->name);
if ($xml->nodeType == XMLReader::END_ELEMENT)
array_pop($a);
if ($xml->nodeType == XMLReader::TEXT) {
if (!in_array(implode(",", $a), $result)) {
$result[] = implode(",", $a);
}
}
}
這似乎是工作,但沒有按」 t用子節點打印節點,例如:
Invoices
Invoices, Company
Invoices, Documents
Invoices, Documents, Document
您需要遍歷每個節點的孩子也是如此。 – jhmckimm
@PaulCrovella我的錯誤。我習慣於使用[SimpleXML](http://php.net/manual/en/book.simplexml.php),因此我的錯誤假設。 – jhmckimm