How can I create XML nodes with an XML index using XML :: LibXML?

I am adding nodes to my XML document as part of some internal processing, but I cannot get XML::LibXML to automatically back up the added nodes.

I get the output as follows:

Here is what I am getting now 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 , does not seem to help.

+6
source share
2 answers

I played around with the settings a bit 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:

 <?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> 
+6
source

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

+1
source

Source: https://habr.com/ru/post/896040/


All Articles