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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:34:58+00:00 2026-05-12T07:34:58+00:00

I am new here and to PHP, i am trying to write a xml

  • 0

I am new here and to PHP, i am trying to write a xml file whitch had as source a XML file.

When i read the XML and use the echo command I can see al the record of the source XML.

but when i want to write it to a XML file i can only see one record.

I have read al lot of things already:
some thing i found was the use array’s, the use of xpath, or simpleXML.
For the code in echo part i use foreach is this possible with DOM elements?

The code is: `

$objDOM = new DOMDocument();

$dom1 = new DOMDocument();

$objDOM->preserveWhiteSpace = false;

$objDOM->load(“googlepoints.xml”); //make sure path is correct

$photo = $objDOM->getElementsByTagName(“photo”);

foreach ($photo as $value) {
$album = $value->getElementsByTagName(“album”);
$albu = $album->item(0)->nodeValue;

$description = $value->getElementsByTagName("description");
$descriptio = $description->item(0)->nodeValue;

$title = $value->getElementsByTagName("title");
$titl = $title->item(0)->nodeValue;

$link = $value->getElementsByTagName("link");
$lin = $link->item(0)->nodeValue;

$guid = $value->getElementsByTagName("guid");
$gui = $guid->item(0)->nodeValue;

$gps = $value->getElementsByTagName("gps");
$gp = $gps->item(0)->nodeValue;

echo "$task :: $detail :: $albu :: $descriptio :: $titl :: $lin :: $gui :: $medi :: $gp  <br>";


// create doctype
$dom1 = new DOMDocument("1.0", 'utf-8');
$dom1->formatOutput = true;

    $collection = $dom1->appendchild($dom1->createElement("collection"));

        // create child element "photo"
        $photo = $collection->appendChild($dom1->createElement("photo"));


        // create child element "album"
        $album = $photo->appendChild($dom1->createElement("album"));
        $album->appendChild($dom1->createTextNode("$albu"));

        // create child element "description"
        $description = $photo->appendChild($dom1->createElement("description"));
        $description->appendChild($dom1->createTextNode("$descriptio"));

        // create child element "title"
        $title = $photo->appendChild($dom1->createElement("title"));
        $title->appendChild($dom1->createTextNode("$titl"));

        // create child element "link"
        $link = $photo->appendChild($dom1->createElement("link"));
        $link->appendChild($dom1->createTextNode("$lin"));

        // create text node
        $guid = $photo->appendChild($dom1->createElement("guid"));
        $guid->appendChild($dom1->createTextNode("$gu"));

        // create child element "gps"
        $gps = $photo->appendChild($dom1->createElement('gps'));
        //$gps->appendChild($dom1->createTextNode("$gp"));
        $gps->appendChild($dom1->createTextNode("$gp"));

}
// save tree to file
$dom1->save(“order12.xml”);

    //// save tree to string
    //$dom1 = $dom1->save("order77.xml");

?>`

Out put of the XML file<?xml version="1.0" encoding="utf-8"?>
<collection>
<photo>
<album>Landschap</album>
<description>Foto's genomen op een mooie koude winterdag.</description>
<title>nil</title>
<link>index.html</link>
<guid></guid>
<gps>22°39'5" N 5°40'54" E</gps>
</photo>
</collection>

Only there suppose the be 5 of this records.

Can sombody help me ?

  • 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-12T07:34:58+00:00Added an answer on May 12, 2026 at 7:34 am

    The $dom1 variable needs to be set outside of the foreach loop.

    i.e.

    $dom1 = new DOMDocument("1.0", 'utf-8');
    $dom1->formatOutput = true;
    
    foreach($photo as $value) {
    
         $dom1->appendChild(...);
         ...
    }
    
    $xml = $dom1->save();
    

    It’s only one record because you’re overwriting the $dom1 each time where you have the comment // create doctype

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

Sidebar

Related Questions

I am new to php and trying to write a login function. Bit stuck
I'm trying to write a simple PHP script which automatically sets up new etherpads
I'm new to python and pylons although experienced in PHP. I'm trying to write
Here's the code. Not much to it. <?php include("Spreadsheet/Excel/Writer.php"); $xls = new Spreadsheet_Excel_Writer(); $sheet
Here's my binding source object: Public Class MyListObject Private _mylist As New ObservableCollection(Of String)
I have come up to issues while I'm trying to write some classes, here
I am trying to patch my php fusion to a new vulnerability. But I
I am new here so first of all my greetings to you I am
I'm starting a new project here (Windows Forms). What's the best option today for
Based on my previous question here my new concern is how do I unit

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.