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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:21:17+00:00 2026-05-13T06:21:17+00:00

I asked a similar question last week but did not get an answer that

  • 0

I asked a similar question last week but did not get an answer that really nailed it. I suspect the question needs to be stated more plainly so here goes:

Given this XML:

<?xml version="1.0" encoding="utf-8"?>
<everyone>
  <guest>
    <name>Joseph Needham</name>
    <age>53</age>
  </guest>
  <guest>
    <name>Lu Gwei-djen</name>
    <age>31</age>
  </guest>
</everyone>

How do I return precisely this:

  <guest>
    <name>Joseph Needham</name>
    <age>53</age>
  </guest>
  <guest>
    <name>Lu Gwei-djen</name>
    <age>31</age>
  </guest>

I don’t want a simplexml object, I don’t want to convert anything, I don’t want just the values of nodes, I don’t want a new XML document with its corresponding header… just that chunk of XML. Can’t use external libraries… nothing that doesn’t come with a standard, average PHP install. How do I extract one out of the other?

My best guess? Use DomDocument to get the nodes names and content somehow and then rebuild what I want by using a foreach loop and echoing out the various node names and values including line endings to format everything properly. However, this seems like it could be amazingly clunky. I suspect there is a simpler way to do it so I want to see if anyone here on stackoverflow know what that way is (or can tell me that there is, in fact, NOT an easier way). Thanks in advance.

  • 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-13T06:21:17+00:00Added an answer on May 13, 2026 at 6:21 am
    $string = <<<XML
    <?xml version="1.0" encoding="utf-8"?>
    <everyone>
      <guest>
        <name>Joseph Needham</name>
        <age>53</age>
      </guest>
      <guest>
        <name>Lu Gwei-djen</name>
        <age>31</age>
      </guest>
    </everyone>
    
    XML;
    
    $xml = new SimpleXMLElement($string);
    $nodes = $xml->xpath('/everyone/guest');
    
    $result = '';
    foreach ( $nodes as $node ) {
      $result .= $node->asXML()."\n";
    }
    echo $result;
    die;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a similar question about this previously, but I did not specify that
I already asked a similar question, PE Header requirements , but I'm not really
I asked a similar question last week and didn't get a very good response,
I asked a similar question recently, but didn't get a clear answer because I
This is similar to this question I asked last week. My dataGrid is populated
I asked a similar question yesterday, but recognize that i need to rephase it
I have asked a similar question before, but didn't get very good results. I've
I asked a similar question previously, but it was so vague that I couldn't
I asked a similar question, but I couldn't get it working exactly. I'm building
I asked a similar question yesterday that was specific to a technology, but now

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.