Developer
Developer

Reputation: 6350

How to format xml created from XML::LibXML in perl

I am creating XML data in Perl by using XML::LibXML Module But when i am writing the data into a file want to Pretty-printing it so that is it can be easily readable .

Below is a snapshot how i am creating xml from my perl script:

my $xml = XML::LibXML::Document->new('1.0', 'UTF-8');
$xml->createElement('A');
$elem->setAttribute('B',data)

Is there any way we can format the XML by using XML::LibXML because i have to stick with this module only.

Upvotes: 1

Views: 447

Answers (1)

collapsar
collapsar

Reputation: 17238

The method XML::LibXML::Document::serialize writes the xml document as text. Its parameter allows for limited control over the format of the output.

XML::LibXML is a veneer to the libxml2 system library. This library comes with a hard-coded indentation of 2 spaces, so unless you create your own pretty-printer your options will be limited.

However, there are a number of standalone utilities that reformat syntactically valid xml and allow more fine-grained control and which can be run as a postprocessor from within perl on a file with the serialized xml. I've been satisfied with xmlstarlet and xmllint.

Another question is whether you really want to invest many resources into the endeavour. If you need the human-readable version for debugging or out-of-order inspection only, loading the data into a browser like Chrome or Firefox may be enough - they run xml data through a very decent pretty-printer.

Upvotes: 2

Related Questions