1

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?

2
  • 1
    Instead of using load(), use the loadXML() method. Commented Oct 12, 2016 at 15:52
  • @salathe It works, thanks Commented Oct 12, 2016 at 15:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.