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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:06:40+00:00 2026-05-15T10:06:40+00:00

I am creating XML using a while loop, but now I need to prepend

  • 0

I am creating XML using a while loop, but now I need to prepend and append the generated XML with the XML header info and wrapper tag, but I am struggling to get it to work, here is my code,

$result = mysql_query("SELECT * FROM users")
or die(mysql_error());

$row = mysql_fetch_array($result);

    while ($row = mysql_fetch_array($result)) {
        $pumaXML  = "<userDetails>";
        $pumaXML .= "<userID>".$row['uid']."</userID>";
        $pumaXML .= "<userName>".$row['userName']."</userName>";
        $pumaXML .= "<points>".$row['points']."</points>";
        $pumaXML .= "<imageURL>".$row['imageURL']."</imageURL>";
        $pumaXML .= "<thumbURL>".$row['thumbURL']."</thumbURL>";
        $pumaXML .= "</userDetails>";
    };

I can’t seem to find a way to do this, I also tried making a function, but that didn’t go that well, here is that code,

function createXML($result) {
    while ($row = mysql_fetch_array($result)) {
        $pumaXML  = "<userDetails>";
        $pumaXML .= "<userID>".$row['uid']."</userID>";
        $pumaXML .= "<userName>".$row['userName']."</userName>";
        $pumaXML .= "<points>".$row['points']."</points>";
        $pumaXML .= "<imageURL>".$row['imageURL']."</imageURL>";
        $pumaXML .= "<thumbURL>".$row['thumbURL']."</thumbURL>";
        $pumaXML .= "</userDetails>";
    };
    return $pumaXML;
};

Thanx 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-15T10:06:42+00:00Added an answer on May 15, 2026 at 10:06 am

    Here is how to do it with DOM:

    function createUserDetailsXml(array $result) {
    
        $dom  = new DOMDocument;
        $dom->formatOutput = TRUE; // enable automatic indenting
        $dom->loadXML('<users/>'); // set root node
    
        foreach($result as $row) {
    
            // create user-details node
            $user = $dom->createElement('user-details');
    
            // create and append details to user-details node
            $user->appendChild(
                $dom->createElement('user-id', $row['uid']));
            $user->appendChild(
                $dom->createElement('user-name', $row['userName']));
            $user->appendChild(
                $dom->createElement('user-points', $row['points']));
            $user->appendChild(
                $dom->createElement('image-url', $row['imageURL']));
            $user->appendChild(
                $dom->createElement('thumb-url', $row['thumbURL']));
    
            // add user-details node to XML document, e.g. users node
            $dom->documentElement->appendChild($user);
        };
        return $dom->saveXML(); // returns the formatted XML
    };
    

    Note that the function expects you to pass in the full result array, so I could test it with:

    $result = array(
        array(
            'uid'      => 1,
            'userName' => 'Gordon',
            'points'   => PHP_INT_MAX,
            'imageURL' => 'http://example.com/gordon.jpg',
            'thumbURL' => 'http://example.com/t_gordon.jpg'
        ),
        array(
            'uid'      => 2,
            'userName' => 'John <blink>"Frigging"</blink> Doe',
            'points'   => 0,
            'imageURL' => 'http://example.com/johndoe.jpg',
            'thumbURL' => 'http://example.com/t_johndoe.jpg'
        )
    );
    echo createUserDetailsXml($result);
    

    The function will then return

    <?xml version="1.0"?>
    <users>
      <user-details>
        <user-id>1</user-id>
        <user-name>Gordon</user-name>
        <user-points>2147483647</user-points>
        <image-url>http://example.com/gordon.jpg</image-url>
        <thumb-url>http://example.com/t_gordon.jpg</thumb-url>
      </user-details>
      <user-details>
        <user-id>2</user-id>
        <user-name>John &lt;blink&gt;"Frigging"&lt;/blink&gt; Doe</user-name>
        <user-points>0</user-points>
        <image-url>http://example.com/johndoe.jpg</image-url>
        <thumb-url>http://example.com/t_johndoe.jpg</thumb-url>
      </user-details>
    </users>
    

    Please notice that DOM escaped the special chars in John Doe’s name for you automatically. DOM will also make sure the XML element names (or attributes if you use them) are syntactically valid. It also added the XML Prolog.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 529k
  • Answers 529k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, ... The controls attribute is a boolean attribute. If… May 16, 2026 at 11:24 pm
  • Editorial Team
    Editorial Team added an answer Is it possible for you to use a block instead… May 16, 2026 at 11:24 pm
  • Editorial Team
    Editorial Team added an answer If you're happy with the results being typed as IEnumerable<IEnumerable<T>>… May 16, 2026 at 11:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

At work I am parsing large XML files using the DefaultHandler class. Doing that,
I have been developing web apps for a while now and for the past
For purposes of testing compression, I need to be able to create large files,
I’m having a WCF REST service hosted in IIS using .NET 4 RC. The
I'm engaged in a debate over XML representations of groups of objects. Given an
I have web application using SpringFramework3.0.3.RELEASE, developed in Eclipse with m2eclipse plugin and deployed
I am interested in creating a dynamic component in JSF. By that I mean
This subject has been touched on before, but there's been some time since the
I'm really new to Java and I have to create an applet for signing
I am having a lot of trouble with Reflection in C# at the moment.

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.