Converting from Xml Name-Values into Simple Hash

converting from xml name-values into simple hash

There are a few libraries you can use in Ruby to do this.

Ruby toolbox has some good coverage of a few of them:

https://www.ruby-toolbox.com/categories/xml_mapping

I use XMLSimple, just require the gem then load in your xml file using xml_in:

require 'xmlsimple'
hash = XmlSimple.xml_in('session.xml')

If you're in a Rails environment, you can just use Active Support:

require 'active_support' 
session = Hash.from_xml('session.xml')

Turning XML from HTTP request into hash

You can parse normal data as well using SimpleXML gem. Assuming you have installed xml-simple gem already, do

require 'xmlsimple'
xml_str = "<data><title>The X-Files</title><episodes>202</episodes><ended>Yes</ended></data>"
xml_dict = XmlSimple.xml_in(xml_str)

The xml_str variable can be constructed from the http response.

Note that the current data you have <title>The X-Files</title><episodes>202</episodes><ended>Yes</ended> is not valid xml (it looks valid markup but is not, you can check here) and so I had to wrap them up within another parent <data> tag of its own.

How do I convert ruby hash to proper xml format

See Nokogiri::XML::Builder.

Note that your example is not a valid XML document because it is missing a root element. Assuming you want a document rooted with an element named object:

require 'nokogiri'

h = {
name: 'Test name' ,
values: [123, 456, 789],
}

builder = Nokogiri::XML::Builder.new do |xml|
xml.object {
xml.name h[:name]
xml.values {
h[:values].each { |v| xml.elements v }
}
}
end

puts builder.to_xml
# <?xml version="1.0"?>
# <object>
# <name>Test name</name>
# <values>
# <elements>123</elements>
# <elements>456</elements>
# <elements>789</elements>
# </values>
# </object>

REXML - How to extract a single element

Try

xmlDoc.elements().to_a('file/name').first.text

and then add some error treatment (this is not robust).

The to_a returns an array of REXML elements. With first you retrieve the
first (and supposedly the only) element. With text you access that elements text content.

Ruby/REXML: Change a tag value from XPath

Using Chris Heald answer I managed to do this with REXML - no Nokogiri necessary. The trick is to use XPath.each instead of XPath.first.

This works:

require 'rexml/document'
include REXML

xmlfile = File.new("some.xml")
xmldoc = Document.new(xmlfile)

XPath.each(xmldoc, "/config/name") do|node|
p node.text # => So and so
node.text = 'Something else'
p node.text # => Something else
end

xmldoc.write(File.open("somexml", "w"))

Inserting XML from XDocument into databse column with header and no visible linefeeds

You should not think about XML as text with some fancy extras... How is this stored in SQL Server? If the target column is a real XML type you should not bother about the visual format at all. If the visual format is of any importance, than it is the problem of the consuming / reading software...

If you store the XML in a string typed column, you can store literally everything, even invalid XML. If you want to use this XML in SQL Server with XML methods like .value() or .nodes() you will need the real XML type... If you can control this, make sure the target is a real XML typed variable or column!

The xml declaration (the processing instruction stating the encoding and the xml-version in most cases) will be ommitted in any case. SQL Server does not accept such a declaration with its native XML type.

You get into troubles, if you store the XML in a string typed column together with an xml declaration. In this case you should use encoding="utf-16" and you must store this in a NVARCHAR(MAX) column.

If the actual encoding is NCHAR or NVARCHAR SQL Server expects a unicode encoded string. If the column is without the N, SQL Server expects extended ASCII (collation dependant). You cannot mix this! You cannot convert a string with an xml-declaration stating utf-16 if it is a VARCHAR (and vice-versa).

Anyway one should avoid ASCII encoded XML. This will get you into troubles with non-latin characters and will need expensive operations due to the fact, that SQL Server stores XML in a unicode based tree structure internally.

About namespaces, if there are any, you must be very carefull. They must be part of the XML, otherwise you won't be able to read the XML later.

In this answer you find the code to convert an XDocument into XmlDocument. Then use the property OuterXml to get the textual representation of the XML. As C# internally uses unicode strings, just pass this over into a variable or column of type XML or NVARCHAR(MAX).



Related Topics



Leave a reply



Submit