簡體   English   中英

幫助在PHP中的DOMDocument類

[英]help with DOMDocument class in php

這是我的xml DOM。

        <entities>
            <entity id="7006276">
                <title>xxx</title>
                <entities>
                    <entity id="7877579">
                       <title>xxx</title>
                       <entities>

我想獲取ID為7006276的“實體”,以便我可以訪問其子級“實體”以為其創建一些“實體”元素。

我努力了:

 $this->xmlDocument = new DOMDocument();
 // some code creating the above elements (you dont have to care about this comment code...it just creates the above xml structure
 // $this->xmlDocument->createElement('entity');
 // $sourceEntityElement->appendChild($newEntityElement);
 // and so on...

 // now i want to get the entities mentioned...
 $xmlEntities = $this->xmlDocument->getElementById('7006276')->entities;

但它似乎不起作用。 任何想法我如何得到它,以便我可以創建更多的“實體”元素?

現在,當您調用DOMDocument::getElementById時,PHP不知道要查找什么屬性。 根據getElementById文檔 ,您需要使用DOMElement::setIdAttribute設置有關PHP的id屬性的信息,或者使用DOMDocument::validate針對DTD驗證文檔。

您可以使用xpath通過屬性選擇元素。 您將不得不將其轉換為simplexml並檢查以確保正確加載。

沒有運行這個,但是這里是基本概念:

$sxe = simplexml_import_dom($this->xmlDocument);

$data = $sxe->xpath('//entity[@id="700627"]');

    foreach ($data as $value)
    {
           $value->addChild('entity');
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM