如何使用XML :: LibXML自动缩进XML节点? [英] How can I auto-indent XML nodes with XML::LibXML?

查看:103
本文介绍了如何使用XML :: LibXML自动缩进XML节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在内部处理中将节点添加到XML文档中,但是无法获取

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.

我得到如下输出:

这是我现在通过$xml->toString( 1 )获得的东西:

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>


中记录的toString()方法的可选$format参数XML::LibXML::Document 似乎无济于事.


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

推荐答案

我在设置上玩了一点,这似乎可行:

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);

结果是这样

<?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>

这篇关于如何使用XML :: LibXML自动缩进XML节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆