Solved

writing the xml DOM data into xml file with proper indentation in perl

Posted on 2009-05-12
3
997 Views
Last Modified: 2012-08-13
I am loading the content from an xml file using DOM. I am inserting some elements into the DOM. And now I am  writing the xml using perl XML::DOM::Node class's printToFile method. But i am not getting a properly structured  xml output. Can anyone suggest me about the options available in DOM parser perl module to write a structured xml?
0
Comment
Question by:YUGA255
3 Comments
 
LVL 2

Assisted Solution

by:lyhan
lyhan earned 250 total points
ID: 24364850
I usually use XML::Twig to output the nice formatted XML.

Please see attached code for example
my $parser = new XML::DOM::Parser;

my $dom = $parser->parse($xml);
 

my $book = $dom->createElement("Book");

   $dom->getDocumentElement->appendChild($book);

my $twig = new XML::Twig;

   $twig->set_indent(" "x4);

   $twig->parse($dom->toString);

   $twig->set_pretty_print("indented");

#output

print $twig->sprint;

Open in new window

0
 
LVL 13

Accepted Solution

by:
kawas earned 250 total points
ID: 25225326
late to the party here, but you could use XML::LibXML too
use XML::LibXML;
 

# input is a file

my $input = 'some_file.xml';

# create the parser

my $parser = XML::LibXML->new();
 

# parse a file

my $doc = $parser->parse_file($xml);

# use parse_string() for a string of XML
 

my $filename = 'out.xml';

my $format = 1;
 

# print out the string of formatted XML

print $doc->toString(1);
 

# write to file a string of formatted XML

$doc->toFile($filename, $format);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now