tags:

views:

42

answers:

2

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?

+1  A: 

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.

sczizzo
+1  A: 

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');
    }
Jason Rowe