Jump to content

[SOLVED] Missing xml opening tag


doc1355

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/121911-solved-missing-xml-opening-tag/
Share on other sites

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.