By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,829 Members | 2,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,829 IT Pros & Developers. It's quick & easy.

Missing xml opening tag

P: 16
I have a process.php with the following code:

[PHP]<?php
$xdoc = new DomDocument;
$xdoc->Load('1.xml');
$test = $xdoc->save("2.xml");
?>[/PHP]

It should load 1.xml file, and saves it as 2.xml.
Here is my 1.xml file:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <products>
  3.   <product>
  4.     <sku>10001</sku>
  5.     <ti>Searchlight</ti>
  6.     <qty>3</qty>
  7.     <price>822.51</price>
  8.     <st>1</st>
  9.   </product>
  10.   <product>
  11.     <sku>10002</sku>
  12.     <ti>Radio</ti>
  13.     <qty></qty>
  14.     <price>320.86</price>
  15.     <st></st>
  16.   </product>
  17. </products>
and here is my 2.xml:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <products>
  3.   <product>
  4.     <sku>10001</sku>
  5.     <ti>Searchlight</ti>
  6.     <qty>3</qty>
  7.     <price>822.51</price>
  8.     <st>1</st>
  9.   </product>
  10.   <product>
  11.     <sku>10002</sku>
  12.     <ti>Radio</ti>
  13.     </qty>
  14.     <price>320.86</price>
  15.     </st>
  16.   </product>
  17. </products>
When I look at 2.xml, if there was a child element that had no value (<qty> and <st>) in 1.xml, it shows only as </qty> and </st> in 2.xml. The opening tag is not there. Is there anything I'm doing worng ? how can I fix this?

Thanks
Aug 29 '08 #1
Share this Question
Share on Google+
2 Replies


P: 16
Anybody has any idea about this? There should be a way to fix this problem?
Aug 29 '08 #2

Atli
Expert 5K+
P: 5,058
Hi.

There is technically nothing wrong there.
When you load the XML into the DomDocument object it is parsed into nodes.
Then, when you save it, it gets parsed again into a XML file. The empty tag gets truncated into a single closing tag, as an empty tag should be.
(Although I would have expected it to be <tag />, but both are valid I guess.)

If you loaded the second one again and added a value to the tag, it should be saved like you expected it to be.

If you don't want this to happen, try adding a single space to it, see if that keeps it in it's original form.
Aug 30 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.