简体   繁体   中英

Declaring namespaces using libxml-ruby

I am using libxml-ruby for parsing XML.

I can able to create the xml file using libxml-ruby, but the problem is I am not able to declare the namespace for generated XML document.

Please help me how to create the namespace for newly generating XML.

The code written for creating xml is:

require 'rubygems' 
require 'libxml'

filename = 'something.xml'
stats_doc = LibXML::XML::Document.new()
stats_doc.root = LibXML::XML::Node.new('root_node') 
stats_doc.root << LibXML::XML::Node.new('elem1') 
stats_doc.save(filename, :indent => true, :encoding => LibXML::XML::Encoding::UTF_8)

You just have to create a new instance of the XML::Namespace class as described here . Below you can find modified version of your example.

require 'rubygems' 
require 'libxml'

filename = 'something.xml'
stats_doc = LibXML::XML::Document.new()
stats_doc.root = LibXML::XML::Node.new('root_node')

LibXML::XML::Namespace.new(stats_doc.root, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')

stats_doc.root << LibXML::XML::Node.new('elem1')
stats_doc.save(filename, :indent => true, :encoding => LibXML::XML::Encoding::UTF_8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM