0

this is my code and i am trying to show the value of xml using Xpath but when i run this i am getting error in my code.

Here is the code

<?php
$load = new DOMDocument();
$load = simplexml_load_file("testing.xml");
var_dump($load);
$xpath = new DOMXpath($load);
var_dump($xpath);
$path1 = "/clip/metadata[name=keywords]/value";
$query = $xpath->query($path1);
var_dump("$query"); 
?>

this is the error, which i am getting

Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, instance of SimpleXMLElement given in C:\xampp\htdocs\xml-text\index.php on line 5
1
  • No need to use simplexml_load_file, instead you can use load method of the DOMDocument object. Commented Jul 24, 2015 at 14:35

2 Answers 2

2

As the error states, you are not passing the constructor the appropriate arguments. simplexml_load_file returns a SimpleXmlElement object, NOT a DOMDocument object.

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

1 Comment

thanks @thatidiotguy i am totally New with XML Programming through PHP and I am trying to load an xml file and then search within it. can you please give me a tip ,what can i used here. i also have used loadXML(); function but it still didnt work
1

As stated in the error, you are passing a SimpleXmlElement object instead of a DOMDocument object.

My previous answer was incorrect. It showed how to convert a SimpleXmlElement to a DOMElement not a DOMDocument.

http://php.net/manual/en/domdocument.load.php is how to properly load an xml file into a DOMDocument object.

$load = new DOMDocument();
$load->load("testing.xml");
$xpath = new DOMXpath($load);

Specifically to get the value of the node with the name Keywords you would do something like this

$load = new DOMDocument();
$load->preserveWhiteSpace = false;
$load->load(__DIR__ . "/testing.xml");
$xpath = new DOMXpath($load);
$path1 = '//clip/metadata/name[ . = "Keywords"]';
$query = $xpath->query($path1);
foreach($query as $entry) {
    $value = $entry->parentNode->childNodes->item(1)->nodeValue;
}

11 Comments

Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given in C:\xampp\htdocs\xml-text\index.php on line 6
thanks @ honerlawd now i am having this error to just to let you know, i am also trying to get it away and reading from the Link , what you have posted
@kunal I fixed my answer. I realized I was converting from SimpleXmlElement to DOMElement not DOMDocument.
thanks @honerlawd i seen your change and implementing it.
<?xml version="1.0" encoding="UTF-8"?> <clip> <supplier>people</supplier> <metadata> <name>Owner</name> <value>Self-Company</value> </metadata> <metadata> <name>Keywords</name> <value>school: teacher, student classes</value> </metadata> </clip>
|

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.