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 9241571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:23:35+00:00 2026-06-18T08:23:35+00:00

I have been stuck here for a while .I get the node value in

  • 0

I have been stuck here for a while .I get the node value in an xml using getElementsByTagName. But i face a problem when the node has child nodes. Now when i use the tag getElementsByTagName , i want to even print the subnodes and its values.

ex:-
   <data>
  <details>SOme details </details>
 <item> 
  <title>Top Tories urge gay marriage support</title>  
  <description>Three senior Conservative ministers urge party MPs to drop any opposition to allowing gay marriage in England and Wales, ahead of a Commons vote.</description>  
  <link>http://www.bbc.co.uk/news/uk-politics-21325702#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
  <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-politics-21325702</guid>  
  <pubDate>Tue, 05 Feb 2013 05:04:20 GMT</pubDate>  
  <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65702000/jpg/_65702114_65557953.jpg"/>  
  <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65702000/jpg/_65702115_65557953.jpg"/> 
</item>  
<item> 
  <title>'Independence day' plans revealed</title>  
  <description>The Scottish government has drawn up detailed plans for a transition to independence in March 2016 if there is a "yes" vote, the BBC can reveal.</description>  
  <link>http://www.bbc.co.uk/news/uk-scotland-scotland-politics-21331302#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
  <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-scotland-scotland-politics-21331302</guid>  
  <pubDate>Tue, 05 Feb 2013 05:31:51 GMT</pubDate>  
  <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65700000/jpg/_65700866_013673187-1.jpg"/>  
  <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65700000/jpg/_65700988_013673187-1.jpg"/> 
</item>  
<item> 
  <title>Reconstructed face of Richard III</title>  
  <description>A facial reconstruction of how Richard III may have looked is released, after archaeologists confirmed a skeleton found beneath a Leicester car park was that of the English king.</description>  
  <link>http://www.bbc.co.uk/news/uk-england-leicestershire-21328380#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
  <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-england-leicestershire-21328380</guid>  
  <pubDate>Mon, 04 Feb 2013 23:03:16 GMT</pubDate>  
  <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65701000/png/_65701188_richardiii.png"/>  
  <media:thumbnail width="144" height="81"   url="http://news.bbcimg.co.uk/media/images/65701000/png/_65701189_richardiii.png"/> 
</item>  
</data>

I am getting output as :-

somedata1somedata2somedata3somedata4.

I want output as :-

 subnode1:-somedata1
 subnode2:-somedata2
 subnode3:-somedata3
 subnode4:-somedata4

My code is:-

   $dom->load($url);//url of the xml file
    $link = $dom->getElementsByTagName($tag_name);//tag_name is the node name
    $value = array();

    for ($i = 0; $i < $link->length; $i++) {
        $childnode['name'] = $link->item($i)->nodeName;
        $childnode['value'] = $link->item($i)->nodeValue;
        echo "Name : " . $childnode['name'];
        echo "<br /> Value : " . $childnode['value'];
        $value[$childnode['name']] = $childnode['value'];
       }
     print_r($value);
  • 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-18T08:23:36+00:00Added an answer on June 18, 2026 at 8:23 am
    $dom = new DOMDocument('1.0');
    $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?>
    <node>
    <subnode1>somedata1</subnode1>
    <subnode2>somedata2</subnode2>
    <subnode3>somedata3</subnode3>
    <subnode4>somedata4</subnode4>
    </node>');
    $node = $dom->getElementsByTagName('node');
    $subNodes = $node->item(0)->childNodes;
    for ($i = 0; $i < $subNodes->length; $i++) {
        $subNode = $subNodes->item($i);
        if ($subNode->nodeType === XML_ELEMENT_NODE) { 
            printf('%s: %s<br>', $subNode->nodeName, $subNode->nodeValue);
        }
    }
    

    UPD

    $dom = new DOMDocument('1.0');
    @$dom->loadXML('<?xml version="1.0" encoding="UTF-8"?>
    <data>
      <details>SOme details </details>
     <item>
      <title>Top Tories urge gay marriage support</title>
      <description>Three senior Conservative ministers urge party MPs to drop any opposition to allowing gay marriage in England and Wales, ahead of a Commons vote.</description>
      <link>http://www.bbc.co.uk/news/uk-politics-21325702#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa></link>
      <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-politics-21325702</guid>
      <pubDate>Tue, 05 Feb 2013 05:04:20 GMT</pubDate>
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65702000/jpg/_65702114_65557953.jpg"/>
      <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65702000/jpg/_65702115_65557953.jpg"/>
    </item>
    <item>
      <title>\'Independence day\' plans revealed</title>
      <description>The Scottish government has drawn up detailed plans for a transition to independence in March 2016 if there is a "yes" vote, the BBC can reveal.</description>
      <link>http://www.bbc.co.uk/news/uk-scotland-scotland-politics-21331302#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>
      <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-scotland-scotland-politics-21331302</guid>
      <pubDate>Tue, 05 Feb 2013 05:31:51 GMT</pubDate>
    
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65700000/jpg/_65700866_013673187-1.jpg"/>
      <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65700000/jpg/_65700988_013673187-1.jpg"/>
    
    </item>
    <item>
      <title>Reconstructed face of Richard III</title>
      <description>A facial reconstruction of how Richard III may have looked is released, after archaeologists confirmed a skeleton found beneath a Leicester car park was that of the English king.</description>
      <link>http://www.bbc.co.uk/news/uk-england-leicestershire-21328380#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>
      <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-england-leicestershire-21328380</guid>
      <pubDate>Mon, 04 Feb 2013 23:03:16 GMT</pubDate>
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65701000/png/_65701188_richardiii.png"/>
      <media:thumbnail width="144" height="81"   url="http://news.bbcimg.co.uk/media/images/65701000/png/_65701189_richardiii.png"/>
    </item>
    </data>');
    
    $xpath = new DOMXPath($dom);
    $details = $xpath->query('//data/details')->item(0)->nodeValue;
    echo "Details: $details<br><br>";
    $items = $xpath->query('//data/item');
    foreach ($items as $item) {
        $subItem = $item->childNodes;
        for ($i = 0; $i < $subItem->length; $i++) {
            $current = $subItem->item($i);
            if ($current->nodeType === XML_ELEMENT_NODE) { //ELEMENT_NODE
                $name = $current->nodeName;
                $value = ($current->nodeName !== 'thumbnail') ? $current->nodeValue : $current->getAttribute('url');
                printf('%s: %s<br>', $name, $value);
            }
        }
        echo '<br>';
    }
    

    UPD #2

    $data = $dom->getElementsByTagName('data');
    $dataChildren = $data->item(0)->childNodes;
    for ($i = 0; $i < $dataChildren->length; $i++) {
        $child = $dataChildren->item($i);
        if ($child->nodeName === 'details') {
            $details = $dataChildren->item($i);
            printf('%s: %s<br><br>', $details->nodeName, $details->nodeValue);
        }
        if ($child->nodeName === 'item') {
            $itemChildren = $child->childNodes;
            for ($j = 0; $j < $itemChildren->length; $j++) {
                $item = $itemChildren->item($j);
                if ($item->nodeType === XML_ELEMENT_NODE) {
                    $name = $item->nodeName;
                    $value = ($item->nodeName !== 'thumbnail') ? $item->nodeValue : $item->getAttribute('url');
                    printf('%s: %s<br>', $name, $value);
                }
            }
            echo '<br>';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know this is basic but somehow i have been stuck here for some
I have xml that looks like this. I'm been stuck for a while on
I have been stuck here for a good while and seem to nail the
I have been stuck on this for a while now and I cannot seem
I have been stuck on this for a while. i have tried and I
I have been stuck for a long time on this simple but obtuse message
I have been lurking and learning in here for a while. Now i have
I'm been stuck on this problem for a while and I'm pretty sure it
I've been stuck with this problem for quite a while now. I'm building a
It's been a while since I needed help, but today I'm here to basically

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.