6

I'm adding nodes to my XML document as part some in-house processing, but cannot get XML::LibXML to auto-indent the added nodes.

I get output like the following:

Here's what I'm currently getting with $xml->toString( 1 ):

                                    <nested_nodes>
                                        <nested_node>
                                        <configuration>A</configuration>
                                        <model>45</model>
                                        <added_node>
        <ID>
            <type>D</type>
            <serial>3</serial>
            <kVal>3</kVal>
        </ID>
    </added_node>
</nested_node>
                                    </nested_nodes>

What I would like to have is pretty-printed output:

                            <nested_nodes>
                                <nested_node>
                                    <configuration>A</configuration>
                                    <model>45</model>
                                    <added_node>
                                        <ID>
                                            <type>D</type>
                                            <serial>3</serial>
                                            <kVal>3</kVal>
                                        </ID>
                                    </added_node>
                                </nested_node>
                            </nested_nodes>

The optional $format parameter for the toString() method documented in XML::LibXML::Document doesn't seem to help.

Zaid
  • 36,680
  • 16
  • 86
  • 155

2 Answers2

7

I played a bit with settings and this seems to work:

use XML::LibXML;

my $doc = XML::LibXML->load_xml(string => <<END_XML, { no_blanks => 1 });
                                    <nested_nodes>
                                        <nested_node>
                                        <configuration>A</configuration>
                                        <model>45</model>
                                        <added_node>
        <ID>
            <type>D</type>
            <serial>3</serial>
            <kVal>3</kVal>
        </ID>
    </added_node>
</nested_node>
                                    </nested_nodes>
END_XML

print $doc->toString(1);

Result is this:

<?xml version="1.0"?>
<nested_nodes>
  <nested_node>
    <configuration>A</configuration>
    <model>45</model>
    <added_node>
      <ID>
        <type>D</type>
        <serial>3</serial>
        <kVal>3</kVal>
      </ID>
    </added_node>
  </nested_node>
</nested_nodes>
bvr
  • 9,687
  • 22
  • 28
  • 1
    Excellent! The trick was to add the `no_blanks` option to both nested and added nodes. Good work @bvr! – Zaid Aug 28 '11 at 10:07
  • Thank you, so much! For the reference, this works form file as well using location => $filename, { no_blanks => 1} – JLZenor Jan 19 '16 at 21:46
1

If you don't mind using another tool, I recommend XML::Tidy. It does one job, and it does it well.

toolic
  • 57,801
  • 17
  • 75
  • 117
  • I have to stick with `XML::LibXML` unfortunately – Zaid Aug 27 '11 at 18:52
  • It's not available on the target platform, and I don't want to go down the road of including the [`XML::Tidy`](http://search.cpan.org/perldoc?XML::Tidy) source as part of my scripts. – Zaid Aug 27 '11 at 23:29