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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:08:43+00:00 2026-05-31T13:08:43+00:00

I have been trying to make a method for adding new elements in an

  • 0

I have been trying to make a method for adding new elements in an XML file, but for some reason it does not modify the element, even that when i Use saveXML prints the right ones.

function fnDOMEditElementCond($product_id, $name, $weight, $category, $location) {

        if (!isset($product_id) || is_numeric($product_id)) {
          return false;
      }
        $obj = new helperClass();
        $xmlDoc = $obj->fetchFromXMLDocument('storage.xml');
        $xpath = new DOMXPath($xmlDoc);

        $result = $xpath->query(sprintf('/storagehouse/item[@id="%s"]', $product_id));
        if (!$result || $result->length !== 1) {
            throw new Exception(sprintf('Item with id "%s" does not exists or is not unique.', $product_id));
        }
        $item = $result->item(0);
       //Change the name element
        $xName = $xpath->query("./name", $item)->item(0);
        $xName->removeChild($xName);

//        //Change the name element
//        $xWeight = $xpath->query("./weight", $item)->item(0);
//        $xWeight->nodeValue = $weight;
//        
//        //Change the name element
//        $xLocation = $xpath->query("./location", $item)->item(0);
//        $xLocation->nodeValue = $category;
//        
//        //Change the name element
//        $xCategory = $xpath->query("./category", $item)->item(0);
//        $xCategory->nodeValue = $location;








        echo $xmlDoc->saveXML($item);
    }

}

This code takes a ID, and depending on the id it modifies the information, on four elements.

Here is the xml fil

storagehouse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="schema.xsd">
    <item id="c7278e33ef0f4aff88da10dfeeaaae7a">
        <name>HDMI Cable 3m</name>
        <weight>0.5</weight>
        <category>Cables</category>
        <location>B3</location>
    </item>
    <item id="df799fb47bc1e13f3e1c8b04ebd16a96">
        <name>Dell U2410</name>
        <weight>2.5</weight>
        <category>Monitors</category>
        <location>C2</location>
    </item>
    <item id="53abbd89766ea8759b5ebe5bacd43f58">
        <name>HP Probook 1311</name>
        <weight>2.1</weight>
        <category>Notebooks</category>
        <location>A1</location>
    </item>
<storagehouse/>

Do you have any idea on why that it might not working?

  • 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-31T13:08:43+00:00Added an answer on May 31, 2026 at 1:08 pm

    I believe it is because you are looking for save() not savexml(). And you will need to give save() the filename.

    Edit: Also, if I’m remembering correctly, you will need to reload the DOM after each save, otherwise you will only effect the unsaved DOM elements. But it’s been a while since I’ve had to do it. I’ll take a look through my old code to double check.

    Edit Again: I’ve been looking through my code, and it does appear to be necessary. You can do all the modifications you need at once, but if you need to view the results in the interim, you either have to produce it with saveXML() or save() it and read it into a fresh XML object. The original object will only reflect the original content. I didn’t properly note it in my comments like I should have, but my code is done in such a way that it makes me think I am right. Hopefully this helps.

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

Sidebar

Related Questions

Have have been trying to make a validator for my xml files. I have
I have been trying to make it work with either fwrite or fputcsv but
I have been trying to make a method where the user can input a
I have been trying to make wall post from multiple pages but when i
I have been trying to make a case for using Python at my work.
I have been trying to make a div fade in evey 30sec and out
I have been trying to make the sub-menu horizontal. In my HTML it looks
I have been trying to make an init script using start-stop-daemon. I am stuck
I have been trying to make this to be a little jQuery plugin that
I have been trying to make a Cross-platform 2D Online Game, and my maps

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.