Missing xml opening tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doc1355
    New Member
    • Sep 2007
    • 16

    Missing xml opening tag

    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:

    Code:
    <?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:

    Code:
    <?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
  • doc1355
    New Member
    • Sep 2007
    • 16

    #2
    Anybody has any idea about this? There should be a way to fix this problem?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

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

      Comment

      Working...