How to remove the <opt> tag in XML :: Simple output?

I am creating an XML file using Perl and XML :: Simple . I am successfully creating an XML file, but the problem is that I have a <opt> </opt> for each of my tags. I am looking for any option that we can use in the <opt> </opt> . I can not do post processing to remove the tag. because the file size is huge.

Example:

 <opt> <person firstname="Joe" lastname="Smith"> <email> joe@smith.com </email> <email> jsmith@yahoo.com </email> </person> <person firstname="Bob" lastname="Smith"> <email> bob@smith.com </email> </person> </opt> 

and I'm looking (without the <opt> ):

  <person firstname="Joe" lastname="Smith"> <email> joe@smith.com </email> <email> jsmith@yahoo.com </email> </person> <person firstname="Bob" lastname="Smith"> <email> bob@smith.com </email> </person> 
+4
source share
3 answers

A tag is an XML root element created from a user-provided data structure. From an XML document :: Simple -

RootName => 'string' # out - handy

By default, when XMLout () generates XML, the root element will be called "Non-automatic." This parameter allows you to specify an alternate name.

Specifying either undef or an empty string for the RootName parameter will create XML without root elements. In most cases, the resulting XML fragment will not be β€œwell formed” and therefore XMLin () could not be read. However, this option has been found useful in certain circumstances.

To set the root element to empty, simply pass the RootName as "undef" to XMLout, for example,

 use XML::Simple; my $xml = XMLout($hashref, RootName => undef); 
+13
source

I came across this answer when searching for the same information (reading, parsing, modifying and outputting xml, fixing the <opt> root tag), but in Ruby.

FYI, the root of the node can also be removed or named in the Ruby version in the library:

 require 'xmlsimple' # gem install xml-simple data = XmlSimple.xml_in(filename) # read data from filename # Parse data as needed, then output: XmlSimple.xml_out(data, { 'RootName' => nil }) # Remove root element XmlSimple.xml_out(data, { 'RootName' => 'html' }) # Change root <opt> to <html> 
+1
source

This answer did not help me. What you can do is:

 my $xml = XML::Simple->new(KeepRoot=>0); print $xml->XMLout($YourVariable); 

However, a valid XML document must have a root. If what you want to do is the name of your root node, you can do this:

 print $xml->XMLout({'RootNodeName' => {'ChildNode'=>[@ArrayOfThings]}}); 
0
source

All Articles