5

test.html

<html> <body> <span> hello Joe</span> <span> hello Bob</span> <span> hello Gundam</span> <span> hello Corn</span> </body> </html> 

PHP file

$doc = new DOMDocument(); $doc->loadHTMLFile("test.html"); $xpath = new DOMXPath($doc); $retrieve_data = $xpath->evaluate("//span"); echo $retrieve_data->item(1); var_dump($retrieve_data->item(1)); var_dump($retrieve_data); 

I am trying to use xPath to find the spans and then echo it, but it seems I cannot echo it. I tried dumping it to see if is evaluating properly, and I am not sure what does this output mean:

object(DOMElement)#4 (0) { } object(DOMNodeList)#7 (0) { } 

What does the #4 and #7 mean and what does the parenthesis mean; What is does the syntax mean?

Update: This is the error I get when I try to echo $retrieve_data; and $retrieve_data->item(1);

Catchable fatal error: Object of class DOMNodeList could not be converted to string 
9
  • Not an answer to your question, but there are PHP DOM libraries that allow querying objects the jQuery way that feels much more natural to me than XPath. I hate XPath. PHPQuery uses the PHP DOM - examples here; SimpleHTMLDOM is string based but also very nice. Commented Aug 29, 2010 at 22:15
  • I don't know PHP's XPath syntax, but have you tried "//span/text()"? Currently, you're selecting the elements, but you want to output the text inside the elements. Commented Aug 29, 2010 at 22:17
  • 1
    @chris_l It's not PHP's XPath Syntax. It's just XPath. Same in any other language, just that PHP only supports XPath 1.0. @Doug your question has nothing to do with XPath. You are asking how to read standard var_dump output. Commented Aug 29, 2010 at 22:23
  • @Pekka I will take a look at SimpleHTMLDOM, but I still want to solve this problem. Commented Aug 29, 2010 at 22:26
  • @Gordon: Ok, but the API around it is different enough, that I can't tell for sure, how it will handle the result (i. e. will it automatically extract the text content from an element...) Commented Aug 29, 2010 at 22:30

4 Answers 4

3
$xpath->evaluate("//span"); 

returns a typed result if possible or a DOMNodeList containing all nodes matching the given XPath expression. In your case, it returns a DOMNodeList, because your XPath evaluates to four DOMElements, which are specialized DOMNodes. Understanding the Node concept when working with any XML, regardless in what language, is crucial.

echo $retrieve_data->item(1); 

cannot work, because DOMNodeList::item returns a DOMNode and more specifically a DOMElement in your case. You cannot echo objects of any kind in PHP, if they do not implement the __toString() method. DOMElement doesnt. Neither does DOMNodeList. Consequently, you get the fatal error that the object could not be converted to string.

To get the DOMElement's values, you either read their nodeValue or textContent.

Some DOM examples by me: https://stackoverflow.com/search?q=user%3A208809+dom

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

Comments

2

If you want to output text inside span you can use textContent property:

echo $retrieve_data->item(1)->textContent;

2 Comments

I was trying echo $retrieve_data->item(1); earlier. Should that not echo the span html? Also, where can I find the documentation for the textContent part? I did not know you can do that.
Just like VdesmedT said below item(1) is not a string - it's Node object which may include a lot of stuff besides text and name. Base class for all nodes is described (with all available properties) here: php.net/manual/en/class.domnode.php
1

your item is as DOMNode object, echo its nodeValue property might helps

2 Comments

I have no idea what you juts said. What is its nodeValue? Like item(1)?
echo $retrieve_data->item(1)->nodeValue;
1

If you want to output the XML (or HTML rather), try:

echo $doc->saveXML( $retrieve_data->item(1) ); 

BTW, the DOMNodeList, that is the result of your query, is zero base indexed, so the first item would be 0. But perhaps you knew this already.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.