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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:48:40+00:00 2026-06-17T22:48:40+00:00

I have an XML file like below. I have to use the DOMDocument, so

  • 0

I have an XML file like below. I have to use the DOMDocument, so no extra classes allowed. (note I have simplified it, the real file has many entries in both saleItems and catalogItems):

<?xml version="1.0"?>
<catalog>
    <saleItems count="3">

        <item name="Xbox 360 Wired Controller" price="19" stock="50" salePrice="12" onSale="yes">
            <image>Xbox360Controller.jpg</image>
            <description>A wired controller for an Xbox 360 Game Console. Comes in either white or black.</description>
        </item>

    </saleItems>

    <catalogItems count="12">
        <item name="Dell Alienware Laptop 15 inch" price="2500" stock="200" salePrice="0" onSale="no">
            <image>alienwareLaptop.jpg</image>
            <description>sample description here</description>
        </item>
    </catalogItems>
</catalog>

I need to do the following:

1) find the node to update based on the name=”” attribute (ex: the only data I have to find the appropriate item is the item name)

2) when it is found, I need to edit the quantity attribute of just that item (subtract 1)

3) when the quantity has been updated, I need the XML file to be saved back to the server.

I’m at whits ends with this, I’m having a hard time getting the $domDocument to cooperate.. This is what I have so far, but it does not seem to work properly at all. I can get the stock and substract it, but my changes aren’t being saved to the file.

foreach( $GLOBALS['catalogItems'] as $item ){
    if( $item->getAttribute('name') == $itemName ){
        // we have a match
        $oldStock = $item->getAttribute("stock");
        $item->setAttribute("stock", $oldStock + $modifier);

        $dom->save('xml/catalog.xml');
    }
}

any help much appreciated!

  • 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-06-17T22:48:41+00:00Added an answer on June 17, 2026 at 10:48 pm

    Solved via PHP chat: https://chat.stackoverflow.com/transcript/message/7402294#7402294

    The problem was that his selector for $GLOBALS['catalogItems'] included DOMText items, which would fail when calling $item->getAttribute('name'). The solution is to make sure $item is actually an item node and then to do the other checks if it an item.

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

Sidebar

Related Questions

I have an XML file which has many section like the one below: <Operations>
I have an xml file like below which I will use to set background
I have a sample xml file which looks like the one below: <Root> <SubOne>
i can use an XML file like below <set xmlns:android=http://schemas.android.com/apk/res/android android:interpolator=@android:anim/decelerate_interpolator> <translate android:fromXDelta=100% android:toXDelta=0%
I have something like the following (shown below) defined in my styles.xml file. But
i have below 2 xml file(fruit.xml and toy.xml).I wanted to use xquery to retrieve
I have a XML file like this <?xml version=1.0 encoding=utf-8?> <Survey Name=Aerosmith's Survey Id=S2
I have an xml file like this: <result> <customer> <id>1</id> <name>A</name> </customer> <customer> <id>2</id>
I have an XML file like this: <SiteConfig> <Sites> <Site Identifier=a /> <Site Identifier=b
I have created an xml file like this: <?xml version=1.0 encoding=utf-8?> <ListView xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent

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.