1

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!

6
  • 2
    You shouldn't do the saving in the traversal loop. Also consider using Xpath in conjunction to DOMDocument. Commented Jan 28, 2013 at 3:22
  • @mario: thanks for the input! so should I save outside the foreach? also I cannot use Xpath -- this is a small part of a larger assignment for a class (before anyone asks yes SO usage is encouraged :) ), and the course unfortunately requires just the use of DOMDocument and XMLReader, both of which I am unfamiliar with. :( Commented Jan 28, 2013 at 3:24
  • Long shot: What's with the variable scope? I see you pull one from the global scope, but is $dom also present? Can you print_r($dom->saveHTML()); the DOM structure after the loop? Is that output updated? (The loop looks fine at first glance). Did you check for file permissions prior saving? (error_reporting? Add some debug statements, "$itemName found", print new calculation result, etc. Commented Jan 28, 2013 at 3:30
  • Check your save path with real path, not a relative path Commented Jan 28, 2013 at 3:32
  • @mario: $dom is used throughougt the file (including in the global scope), but prior to the loop I defined $dom = new DomDocument() and loaded the catalog.xml file into it. when I echo saveHTML() after the loop it spints out the entire XML file in plaintext. Commented Jan 28, 2013 at 3:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much to @Levi Morrison, helped me out in the chat. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.