当前位置: 动力学知识库 > 问答 > 编程问答 >

xml - help with DOMDocument class in php

问题描述:

here is my xml DOM.

 <entities>

<entity id="7006276">

<title>xxx</title>

<entities>

<entity id="7877579">

<title>xxx</title>

<entities>

i want to get the 'entity' with id 7006276 so i could access its child 'entities' to create some 'entity' elements for it.

i have tried:

 $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;

but it doesnt seem to work. any idea how i get it so i can create more 'entity' elements?

网友答案:

Right now, PHP doesn't know what attribute to look for when you call DOMDocument::getElementById. According to the documentation for getElementById, you need to either set inform PHP about your id attribute with DOMElement::setIdAttribute or validate your document against a DTD with DOMDocument::validate.

网友答案:

You can use xpath to select elements by attribute. You would have to convert it to simplexml and check to make sure it loads correctly.

Didn't run this but here is the basic concept:

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

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

    foreach ($data as $value)
    {
           $value->addChild('entity');
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: