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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:16:18+00:00 2026-05-20T10:16:18+00:00

Given the php code: $xml = <<<EOF <articles> <article> This is a link <link>Title</link>

  • 0

Given the php code:

$xml = <<<EOF
<articles>
<article>
This is a link
<link>Title</link>
with some text following it.
</article>
</articles>
EOF;

function traverse($xml) {
    $result = "";
    foreach($xml->children() as $x) {
        if ($x->count()) {
            $result .= traverse($x);
        }
        else {
            $result .= $x;
        }
    }
    return $result;
}

$parser = new SimpleXMLElement($xml);
traverse($parser);

I expected the function traverse() to return:

This is a link Title with some text following it.

However, it returns only:

Title

Is there a way to get the expected result using simpleXML (obviously for the purpose of consuming the data rather than just returning it as in this simple example)?

  • 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-05-20T10:16:19+00:00Added an answer on May 20, 2026 at 10:16 am

    There might be ways to achieve what you want using only SimpleXML, but in this case, the simplest way to do it is to use DOM. The good news is if you’re already using SimpleXML, you don’t have to change anything as DOM and SimpleXML are basically interchangeable:

    // either
    $articles = simplexml_load_string($xml);
    echo dom_import_simplexml($articles)->textContent;
    
    // or
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    echo $dom->documentElement->textContent;
    

    Assuming your task is to iterate over each <article/> and get its content, your code will look like

    $articles = simplexml_load_string($xml);
    foreach ($articles->article as $article)
    {
        $articleText = dom_import_simplexml($article)->textContent;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My clients home page is https://www.quakereadykit.com/store/home.php This was given to me by a previous
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
I have been given an SWF to edit a link in the AS code.
I have this code to validate an XML file against an XSD file: $file
From this XML response: http://www.dreamincode.net/forums/xml.php?showuser=335389 I want to grab all of the users friends.
I need to upload a given image using Amazon S3 I have this PHP:
Possible Duplicate: Best XML Parser for PHP I have an XML code like as
Given this XML file: <?xml version=1.0 encoding=UTF-8?> <root> <data> <track clipid=1> <url>http://www.emp3world.com/to_download.php?id=33254</url> <http_method>GET or
When I execute this code I get an error saying PHP Fatal error: Call
What is the best way to take a given PHP object and serialize it

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.