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
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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.