Real World
Real World

Reputation: 1719

Step through DOMDocument tag by tag

Strangely I can't find an answer for this, though it seems like it must have been asked before. I have a DOMDocument in PHP and I want to step through each html tag as if it were a flat document basically. I need to inspect each element looking for names of the tag and specific attribute values. I can't use xpath in this instance i don't think because although the structure of the html remains the same, the attributes can be different depending on when the doc is parsed. My document is a little unusual like this

 <tr class='THIS COULD BE ONE OF THREE DIFFERENT CLASSES' id='UNIQUE ID'>
        <td class='statistics show' >
            <button class="js-hide">Show</button>
        </td>


        <td class='details'>
            <p>
                <span class='home'>
                    <a href='LINK'>TEAM 1</a>                </span>
                  <span class='COULD BE ONE OF TWO DIFFERENT CLASSES'> VARIABLE CONTENT </span>                  <span class='away'>
                    <a href='LINK'>TEAM 2</a>                </span>
                                            </p>
        </td>


    <td class='COULD BE ONE OF THREE CLASS TYPES'>
        VARIABLE CONTENT</td>

        <td class='status'>
                    </td>
    </tr>

There are other tags around the document but there are a number of duplicated sections like that one I would like to pull out. I can't see how xpath would allow me to parse this sensibly so tag by tag is my only option but I can't find the correct way to do it. Any suggestions?

Upvotes: 0

Views: 99

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

you could use getElementsByTagName(*) to get all elements and loop through those.

Upvotes: 1

Related Questions