beginner
beginner

Reputation: 2508

How to parse a XML string as document object(DOM) in php

I want to parse a xml string into as XML document(DOM). I know how to do this when xml is stored in different file, example given at

http://www.w3schools.com/php/php_xml_dom.asp

$xmlDoc = new DOMDocument();
$xmlDoc->load(file.xml);

Now we can access the $xmlDoc as a document object, and we can access and manipulate xml using DOM methods. But I want to parse a xml string as a document.I tried

$xmlstring = '
<users>
  <user>
    <name> nikhil </name>
    <password>1234 </password>
  </user>
  <user>
    <name>akhil </name>
    <password>123 </password> 
  </user>
</users>';
$xmlDoc = new DOMDocument();
$xmlDoc->load($xmlstring);

But this does not work, any ideas?

Upvotes: 1

Views: 2327

Answers (1)

Mohammad
Mohammad

Reputation: 21499

DOMDocument::load — Load XML from a file

As you can see in php manual, load() method only work for loading XML from a file.

If you want to load from string, use DOMDocument::loadXML that load XML from string.

Upvotes: 1

Related Questions