9

I am trying to get a child of a PHP DOMDocument. Say I have a DOM document like this:

<div>
   <h1 ></h1>
   <div id=2></div>
   <div class="test"></div>
...
</div>

I have a index number 3. Then I need to get the element <div class="test"></div>. In the DOMDocument API, there isn't a method like children(3). Is there? How can I get a child with an index?

4
  • you need to use getElementByTag('div') and then use the getAttribute ('class') and put that in if condition to match class='test'. it does require bit of RnD Commented May 18, 2011 at 7:14
  • (related) Best Methods to parse HTML and Noob Question about DOMDocument in PHP Commented May 18, 2011 at 7:17
  • possible duplicate of How get first level of dom elements by Domdocument PHP? Commented May 18, 2011 at 7:22
  • @Gordon.I am actually using HTML dom parser. but it seems not robust when document is big. I will look at those post you refer to..thank you very much for you help. Commented May 18, 2011 at 7:57

4 Answers 4

20

You can use childNodes. This is a property of a DOM element that contains a NodeList containing all the element's children. Ideally you'd be able to do $el->childNodes->item(2) (note that it's 0-based, not 1-based, so 2 is the third item). However, this includes text nodes. So it's hard to predict what number your node will be. This probably isn't the best solution.

You could go with alexn's solution (getElementsByTagName('*')->item(2)), but again this has its drawbacks. If your nodes have child nodes, they will also be included in the selection. This could throw your calculation off.

My preferred solution would be to use XPath: it's probably the most stable solution, and not particularly hard.

You'll need to have created an XPath object with $xpath = new DOMXPath($document) somewhere, where $document is your DOMDocument instance. I'm going to assume that $el is the parent div node, the "context" that we're searching in.

$node = $x->query('*', $el)->item(2);

Note that, again, we're using a 0-based index to find which element in the selection it is. Here, we're looking at child nodes of the top level div only, and * selects only element nodes, so the calculations with text nodes are unnecessary.

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

2 Comments

hi, thanks for you answer. I tried $el->childNode->item(2). it striped off all the html tags. I also tried $x->query('/div/*', $el)->item(2);but notice that the child node is not fixed with div... I need to dynamiclly to get a child level by level down with a serial index like 1 3 4 0.
@Yijie Yes, it should have been just query('*', $el). See updated answer.
5

If you use DOMDocument you can use getElementsByTagName('*') which returns a DomNodeList with all elements in your document. You can then invoke the item function which takes an index as a parameter:

$nodes = $dom->getElementsByTagName('*');
$targetNode = $nodes->item(3);

1 Comment

Hi, thank you for the answer, your solution gets all the element from all descendent nodes. and all html tags are striped off. my intention is to get a child level by level down with a serials of index like 1 3 4 0. Hope this is clear.
1

try this

 foreach($dom->getElementsByTagName('div') as $div) { 
        $class = $div->getAttribute('class');
    } 

now you can match the class or id attribute of that particular div and do what ever. this is not the solution but helps you find the contents and attributes with all divs' . hope it helps.

Comments

1

Try this:

$dom->childNodes->item(3)

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.