1
$xml = new DOMDocument();
$xml_store_inventory = $xml->createElement('store-inventory');  // highest layer
$xml_item = $xml->createElement('item');
$xml_quantity = $xml->createElement('quantity');

$xml->appendChild($xml_store_inventory);
$xml_store_inventory->appendChild($xml_item);
$xml_location->appendChild($xml_quantity);

gives:

<?xml version="1.0"?>
<store-inventory>
  <item>
      <quantity></quantity>
  </item>
</store-inventory>

So, I managed to create the above in PHP using DOM. I've been searching online on how to "populate," but I'm not finding any information on how to do this.

More specifically, I'd like this to look like this

<?xml version="1.0" encoding="UTF-8"?>
<store-inventory
xmlns="http://..."
xmlns:xsi="http://..."
xsi:schemaLocation="http://...">

    <item item-id="abcd">
       <quantity>0</quantity>
    </item>
</store-inventory>

So, I'd like to add/change the following:

  1. change the XML version line to include encoding (scrape this, I figured out --> $xml = new DOMDocument('1.0', 'UTF-8');)
  2. Add additional information to an element. e.g. [item] to [item item-id="abcd"]
  3. Also [quantity] to [quantity]0[/quantity]

Can someone help me with this? TIA!

4
  • Hm I just noticed that. After some visits to my previous posts, it appears that it's for marking solutions to my posts. It didn't occur to me to do so. Sorry, I'm kinda new. Commented Jul 6, 2011 at 18:32
  • No problem, was just a little reminder ;). What I don't understand in your question: "1. change the XML version line to include encoding" I both see a version and encoding in the XML. What would you like to change? Commented Jul 6, 2011 at 18:39
  • @hakre, the third block of code above is what I'd like the XML to look like. I've updated my post so reflect that I've figured out 1. Now I need to figure out 2 and 3! Commented Jul 6, 2011 at 18:41
  • I've added an answer for 2 and 3. Regarding UTF-8 encoding: That's XML default, so no need to actually add it. But it's not a problem to do so either ;) Commented Jul 6, 2011 at 18:46

1 Answer 1

1

You're already pretty close.

2: set an attribute:

// set/add an attribute:
$xml_item->setAttribute('item-id', "abcd");

3: add data while adding a tag/element:

// add an element with data:
$xml_quantity = $xml->createElement('quantity', '0');

2+: Use HTMLSpecialchars to prevent the browser to hide the tags:

echo nl2br(html_specialchars($xml->saveXML(), ENT_QUOTES));
Sign up to request clarification or add additional context in comments.

5 Comments

Hey, thanks for your patience! I just also figured out #2 looking at the DOM manual. I'm still getting used to this "->". It's quite odd to me. Thanks for your help!
Actually 2 more questions for you: for element "store-inventory", are those attributes (xmlns, xmlns:xsi, etc.) separated by a space only inside the bracket? Also, is there a way to echo $xml->saveXML(); to actually print on the browser? I keep having to "view source" code to see my result.
You're welcome and glad I could help you. -> is used to access an objects method/function and properties/variables. All methods and properties of DomDocument are explained in the manual. So as you're already digging in there, you're on the right track. It's a lot to read when you start with DomDocument, but the more you use it, the more easy it will become.
For the second additional question I've updated the answer. For the first one it's unclear to me what you mean. I don't get the namespace declaration for the code you have in your question. However by default those are each an attribute of their own, so separated by space.
Superb. Thanks for the speedy response!

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.