Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:50:09+00:00 2026-06-17T22:50:09+00:00

Alright, so I have an odd case here that I just can’t figure out.

  • 0

Alright, so I have an odd case here that I just can’t figure out.

I want to parse a list on a website. The HTML looks somewhat like this:

<!-- ... -->
<ul id="foo">
    <li data-text="item 1">Blabla</li>
    <li data-text="item 2">Blabla</li>
    <li data-text="item 3">Blabla</li>
    <li data-text="item 4">Blabla</li>
</ul>
<!-- ... -->

Now I want to grab all the list items. I use the DOMDocument-class for that. So far, that works out just fine:

$dom = new DOMDocument();

if (!$dom->loadHTML($html)) {
    die ('Could not parse...');
}

$list = $dom->getElementById('foo');
$items = $list->childNodes;
foreach ($items as $item) {
     print_r($item);
}

But now, I’m looking for a simple method to read out the data-text attribute. What I did was:

foreach ($items as $item) {
     echo $item->getAttribute('data-text');
}

This works just fine for the very first item, but then it crashes the foreach-loop. The output is:

item 1
Fatal error: Call to undefined method DOMText::getAttribute()
in example.php on line 44

What I don’t get here is how calling the getAttribute method changes the context of the foreach loop. So here are two questions:

  1. How can invoking the method screw up my foreach loop? Secondly,
    what’s the most elegant workaround?
  2. I realized I could loop through $item->attributes with yet another
    foreach method, then compare the attribute name to data-text and
    read the value in case of a match, but there surely has to be a
    better way to do so?!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:50:11+00:00Added an answer on June 17, 2026 at 10:50 pm

    The problem is the ul has text nodes as children along with the lis textnodes do not have attribute therefore you get an error. just test if the child is an element node before you try to access its attribute

    foreach ($items as $item) {
             if ($item->nodeType == XML_ELEMENT_NODE)
             echo $item->getAttribute('data-text');
    }
    

    You can also use getElementsByTagName(), although if you have nested lists the lis in them will also be selected.

    $items = $list->getElementsByTagName('li');
    foreach ($items as $item) {
        echo $item->getAttribute('data-text');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright. I am completely stumped here. I have a horizontal scrolling list that, when
Alright, here's an odd one from an MS Access database I'm running. I have
Alright, so I have an app that has a custom folder: I want to
Alright, I have a rather odd question here. I feel much more comfortable writing
Alright I have been trying to figure this out and I read the MSDN
Alright I have an xml document that looks something like this: <xml> <list> <partner>
Alright so I have a link that is clicked...once it is clicked I want
Alright so I have a task, that I have to let the client try
Alright so this is an odd one... I have an application built in ColdFusion
Alright so I have an assignment that I have been completely stumped on. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.