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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:25:52+00:00 2026-05-13T21:25:52+00:00

I’m using SimpleXML to add new images childs for a slideshow tool on my

  • 0

I’m using SimpleXML to add new images childs for a slideshow tool on my website, the code is something like this:

$xml_toload = $_SERVER['DOCUMENT_ROOT']. '/xml/'.$country.'/compact.xml';
$xml = simplexml_load_file($xml_toload); //This line will load the XML file. 

$sxe = new SimpleXMLElement($xml->asXML());
//In this line it create a SimpleXMLElement object with the source of the XML file. 
//The following lines will add a new child and others child inside the previous child created. 
$image = $sxe->addChild('image');
$image->addAttribute('imageURL',$file);
$image->addAttribute('thumbURL',$file);
$image->addAttribute('linkURL',$linkurl);
$image->addAttribute('linkTarget',$linkurl);
$image->addChild('caption',$caption);

$sxe->asXML($xml_toload);

Which is working perfectly to add the new
<image attr="blabla"><caption>imageinfo</caption><image>
child, inside of <imagegallery></imagegalley>

However, I need this childs to go just after <imagegallery>, not before the tag is closed, (one after other), this makes to appear new images, at the last place on the imagegallery slideshow.

So the newest childs that I add, should go before the last it has added to the xml, like

<imagegallery>
<image attr="HEREGOESTHENEWST">
    <caption>description</caption>
</image>
<image attr="THEOLDONE">
    <caption>description</caption>
</image>
</imagegallery>

How can I achieve this?

  • 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-13T21:25:52+00:00Added an answer on May 13, 2026 at 9:25 pm

    SimpleXML doesn’t support that kind of manipulation. Currently, it can only append children. To insert children at arbitrary places in the tree, you need DOM and DOMNode::insertBefore() specifically. The problem is that DOM is verbose and it makes it annoying to use, that’s why when I have to do that kind of things, I use a mixture of SimpleXML and DOM. The result became a library aptly named SimpleDOM: use DOM methods with SimpleXML grammar.

    Also, here’s a few tips that I recommend as good practice:

    • Whenever you create a SimpleXMLElement object, name it after the node it represents. Never name it $xml. “XML” is a markup language, it’s text. If I’d see an $xml var I’d think it contains text.
    • The same applies to your $xml_toload var. It doesn’t contain XML, it contains a filepath. Therefore, it should be named something like $filepath.
    • You don’t have to use addAttribute() or addChild() for everything. It’s often simpler to use the array notation and/or the object notation.

    The result script:

    include 'SimpleDOM.php';
    
    // create an <image/> element
    $image = new SimpleXMLElement('<image/>');
    $image['imageURL']   = $file;
    $image['thumbURL']   = $file;
    $image['linkURL']    = $linkurl;
    $image['linkTarget'] = $linkurl;
    $image->caption      = $caption;
    
    // load the file
    $path_to_file = $_SERVER['DOCUMENT_ROOT']. '/xml/'.$country.'/compact.xml';
    $imagegallery = simpledom_load_file($path_to_file);
    
    // insert the new element before the first <image/>
    $imagegallery->insertBefore($image, $imagegallery->image[0]);
    
    // save the file
    $imagegallery->asXML($path_to_file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small

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.