Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 29th, 2008, 03:22 PM
Newbie
 
Join Date: Sep 2007
Location: USA
Posts: 16
Default 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:

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
Reply
  #2  
Old August 29th, 2008, 05:50 PM
Newbie
 
Join Date: Sep 2007
Location: USA
Posts: 16
Default

Anybody has any idea about this? There should be a way to fix this problem?
Reply
  #3  
Old August 30th, 2008, 07:55 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,777
Default

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

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles