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 <a href="http://www.php.net/manual/en/domdocument.getelementbyid.php" rel="nofollow">documentation for getElementById</a>, 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'); }


  • The parameters dictionary contains a null entry for parameter
  • How to remote debug with Delve debugger in Visual Studio Code
  • Does SmartGit support git-svn?
  • Angular - How to access and replace innerHTML from a directive
  • Connecting Google Cloud SQL with Wordpress on Google Compute Engine
  • keeping track of nested ng-repeat index's
  • How to use the resource module to measure the running time of a function?
  • Msvcr71.dll Msvcp71.dll missing
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Deduce parent class of inherited method in C++
  • Action Pack components in Rails
  • SSL client cert authentication for only some URLs?
  • What Makes These Two Array Adds Different?
  • What is the correct way to synchronize a shared, static object in Java?
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Fail:(TESTMODE) Transactions of this market type cannot be processed on this system
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Hide HTML elements without javascript, only CSS
  • Bigquery event streaming and table creation
  • Cannot upload to OneDrive using the new SDK
  • Transactional Create with Validation in ServiceStack Redis Client
  • Custom Tabgroup Appcelerator
  • iOS: Detect app start via notification press
  • $wpdb not working in file of WordPress plugin
  • Change Inet root folder for iis 7
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • What is Eclipse's Declaration View used for?
  • Fetching methods from BroadcastReceiver to update UI
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Redux, normalised entities and lodash merge
  • Arrays break string types in Julia
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal