doc1355 Posted August 29, 2008 Share Posted August 29, 2008 I have a process.php with the following code: <?php $xdoc = new DomDocument; $xdoc->Load('1.xml'); $test = $xdoc->save("2.xml"); ?> It should load 1.xml file, and saves it as 2.xml. Here is my 1.xml file: <?xml version="1.0" encoding="UTF-8"?> <products> <product> <sku>10001</sku> <ti>Searchlight</ti> <qty>3</qty> <price>822.51</price> <st>1</st> </product> <product> <sku>10002</sku> <ti>Radio</ti> <qty></qty> <price>320.86</price> <st></st> </product> </products> and here is my 2.xml: <?xml version="1.0" encoding="UTF-8"?> <products> <product> <sku>10001</sku> <ti>Searchlight</ti> <qty>3</qty> <price>822.51</price> <st>1</st> </product> <product> <sku>10002</sku> <ti>Radio</ti> </qty> <price>320.86</price> </st> </product> </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 Quote Link to comment Share on other sites More sharing options...
FIREBALL5 Posted August 29, 2008 Share Posted August 29, 2008 So I'm guessing that your code there only copies the XML file? Quote Link to comment Share on other sites More sharing options...
doc1355 Posted August 29, 2008 Author Share Posted August 29, 2008 Will, my origianl code is much more than this. It adds attributes to the 2.xml. But I removed those extra codes only to prevent confusion here. The aboce code should copy the xml without change, but it is dropping the opeing tags for empty attributes. You can try above codes and see the result. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 29, 2008 Share Posted August 29, 2008 my output, with tags closed: <?xml version="1.0" encoding="UTF-8"?> <products> <product> <sku>10001</sku> <ti>Searchlight</ti> <qty>3</qty> <price>822.51</price> <st>1</st> </product> <product> <sku>10002</sku> <ti>Radio</ti> <qty/> <price>320.86</price> <st/> </product> </products> Quote Link to comment Share on other sites More sharing options...
doc1355 Posted August 29, 2008 Author Share Posted August 29, 2008 Yes, if you check the second product, it shows only <st/> and <qty/>. Is this correct or it should look like this: <st></st> <qty></qty> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 TECHNICALLY, empty elements can be written as <elem_name />, such as <img /> or a line break, so I guess it's correct. Quote Link to comment Share on other sites More sharing options...
doc1355 Posted August 29, 2008 Author Share Posted August 29, 2008 Thanks, That is all I wanted to know. You guys rock with your quick responses. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.