This project makes a implementation of W3C DOM working on Ruby. Now, it’s under construction…
Following example shows you how to create a new DOM Document object:
require "vcdom" # the Module object VCDOM can be used as a DOMImplementation object @dom_impl = VCDOM # create a document having no namespace doc = @dom_impl.create_document( nil, "test-element" ) # check doc.document_element.tag_name #=> "test-element" doc.document_element.namespace_uri #=> nil # create a document having namespace doc = @dom_impl.create_document( "http://www.vividcode.info/test_namespace", "test-element" ) # check doc.document_element.tag_name #=> "test-element" doc.document_element.namespace_uri #=> "http://www.vividcode.info/test_namespace"
Following example shows you how to parse XML string and how to serialize DOM objects:
require "vcdom/xml_ls" @ls_impl = VCDOM::XMLLS # get parser parser = @ls_impl.create_ls_parser( :mode_asynchronous, nil ) # set input xml string input = @ls_impl.create_ls_input() input.string_data = "<empty-element/>" # do parse doc = parser.parse( input ) doc.document_element.tag_name #=> "empty-element" # parse other xml string input.string_data = "<test>aaaa</test>" doc = parser.parse( input ) doc.document_element.first_child.node_value #=> "aaaa" # serialize serializer = @ls_impl.create_ls_serializer() serializer.write_to_string( doc ) #=> "<test>aaa</test>"