Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need correct XML module

Status
Not open for further replies.

motte

Programmer
May 31, 2001
155
US
Hi everyone.

I am looking for an XML writing module that will do what I need. I need to write contents of a log file daily to one log file. I tried using XML::Writer but it always appends the <?xml> declaration tag and opening and closing root tags. Is there any package that can add xml to a file everytime it is run w/o always adding the tags that I described?

Mike

EX. I desire...
Code:
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<root>
 <log>
   <time></time>
   <date></date>
 </log>
 <log>
   <time></time>
   <date></date>
 </log>
</root>

EX. I keep getting...
Code:
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<root>
 <log>
   <time></time>
   <date></date>
 </log>
 <log>
   <time></time>
   <date></date>
 </log>
</root>
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<root>
 <log>
   <time></time>
   <date></date>
 </log>
 <log>
   <time></time>
   <date></date>
 </log>
</root>
 
Motte,

It looks like you want to append new sections to the file and XML::Writer thinks you want to write a whole new XML doc at the end of the file.

I see three ways around the problem:

1. Read the xml file into a hash, add you data to the hash and write the entire hash out as an xml file. Easy and quick for small files ... gets nasty as the file gets large.

2. Write the new xml to a temp file, strip off the header line and append the results to your file.

3. In my mind, the easiest is just to write a module that generates the additional xml section you want and appends it to the file.

You might also search CPAN ( ) for a better xml module. XML::Simple meets my needs quite well.
 
zadrfle,

I was working with XML::Simple yesterday and it seemed to read in the xml file (with the exception that it turned the attributes and any content of a node into an anonymous hash). If I add a new record, how do I convert the key in that hash that would be an attribute back into an attribute and the &quot;content&quot; key (which is really cdata) into the values between the open/close tags? Does this make sense? If not, I can post some code from a Dump.

Mike
 
Mike,

Please post

- the code where you read in the XML and set up the hash.

- A sample of the XML file.

I will take a look at it tomorrow at work & see if I can assist.

RichardB
 
zadrfle,

I actually got what I wanted to work so I don't need to really post any code (I think). What I do find strange is this: Why does the data structure seem to change. Sometimes it will be a hash->hash->array or hash->array->hash. This happened to me when doing 2 different scripts on the same type of xml layout (different tag names). The other thing I find weird is that if I have an attribute called &quot;id&quot;, it is read in as &quot;id&quot; but written out as &quot;name&quot;. That could really screw things up. Have you ever encounted this?

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top