rexml

How to get the absolute node path in XML using XPath and Ruby?

谁说胖子不能爱 提交于 2019-12-12 23:33:08
问题 Basically I want to extract the absolute path from a node to root and report it to the console or a file. Below is the current solution: require "rexml/document" include REXML def get_path(xml_doc, key) XPath.each(xml_doc, key) do |node| puts "\"#{node}\"" XPath.each(node, '(ancestor::#node)') do |el| # puts el end end end test_doc = Document.new <<EOF <root> <level1 key="1" value="B"> <level2 key="12" value="B" /> <level2 key="13" value="B" /> </level1> </root> EOF get_path test_doc, "//*/[

Unable to create Videoconferences sessions with Tokbox

最后都变了- 提交于 2019-12-12 03:27:51
问题 The app create sessions for videoconferences and one of the values is a session id that takes from from tokbox. This was working for a lot of time, so in this days started to get the following error many attempts, and many other attempts runs ok, here the message from console: REXML::ParseException (Missing end tag for 'hr' (got "body") Line: 6 Position: 169 Last 80 unconsumed characters: ): All seems that missing content from the tokbox id, no code change while this time lapse since this

How to get the attributes of an XML tag using Ruby with REXML?

你。 提交于 2019-12-12 02:00:07
问题 I want to output an xml file in a specific way only using REXML with Ruby. Here is what the xml file looks like: <desc> <id>2408</id> <who name="Joe Silva">joe@silva.com</who> <when>Today</when> <thetext>Hello World</thetext> </desc> <desc> <id>2409</id> <who name="Joe Silva2">joe2@silva.com</who> <when>Future</when> <thetext>Hello World Again</thetext> </desc> So far, here is the code I use: document.elements.each("//desc") { |e| e.elements.each |i| puts "#{i.name} : #{i.text}" puts "\n" }

REXML - How to extract a single element

怎甘沉沦 提交于 2019-12-10 10:03:18
问题 I am writing some acceptance tests in ruby which involve asserting the presence of values in response XML. My XML is like this: <?xml version="1.0"?> <file xmlns:dvi=xxxxx> <name>whatever</name> </file> There are other attributes but what is above should illustrate my point. Here is my ruby code: xml = <the xml above> xmlDoc = REXML::Document.new(xml) puts xmlDoc.elements().to_a('file/name') This prints out <name>whatever</name> but I want it to simply print out whatever In this XML there

accessing xml file with REXML

江枫思渺然 提交于 2019-12-08 04:37:37
问题 sample.xml <?xml version="1.0" encoding="utf-8"?> <ShipmentRequest> <Message> <Header> <MemberId>MID-0000001</MemberId> <MemberName>Bruce</MemberName> <DeliveryId>0000001</DeliveryId> <OrderNumber>ON-000000001</OrderNumber> <ShipToName>Alan</ShipToName> <ShipToZip>123-4567</ShipToZip> <ShipToStreet>West</ShipToStreet> <ShipToCity>Seatle</ShipToCity> <Payments> <PayType>Credit Card</PayType> <Amount>20</Amount> </Payments> <Payments> <PayType>Points</PayType> <Amount>22</Amount> </Payments>

REXML - How to extract a single element

巧了我就是萌 提交于 2019-12-05 18:26:29
I am writing some acceptance tests in ruby which involve asserting the presence of values in response XML. My XML is like this: <?xml version="1.0"?> <file xmlns:dvi=xxxxx> <name>whatever</name> </file> There are other attributes but what is above should illustrate my point. Here is my ruby code: xml = <the xml above> xmlDoc = REXML::Document.new(xml) puts xmlDoc.elements().to_a('file/name') This prints out <name>whatever</name> but I want it to simply print out whatever In this XML there will only ever be one name element. I have been able to print out just the text I want using elements.each

Ruby Unit Test : Is this a Valid (well-formed) XML Doc?

我们两清 提交于 2019-12-04 03:24:31
问题 I'm creating an XML document: I want to unit test at least to make sure it's well-formed. So far, I have only been able to approximate this , by using the 'hasElements' in the REXML library. Is there a better way ? Preferably using built-in libraries (I mean libraries that ship with the standard Ruby 1.8.x distro). require "test/unit" require 'rexml/document' require 'test/unit/ui/console/testrunner' include REXML class TestBasic < Test::Unit::TestCase def test_createXML my_xml=...create doc

REXML::Document.new can we give encode parameters on this line?

邮差的信 提交于 2019-12-02 16:36:34
问题 doc = REXML::Document.new file My code is failing at this point whenever my xml file contains some special characters other than UTF-8 . REXML::ParseException (#<REXML::ParseException: #<ArgumentError: invalid byte sequence in UTF-8> 回答1: You can call something like this REXML::Document.new(file.force_encoding("FILE_ENCODING").encode("UTF-8")) FILE_ENCODING is the encoding of your file variable. 来源: https://stackoverflow.com/questions/17194393/rexmldocument-new-can-we-give-encode-parameters

Ruby Unit Test : Is this a Valid (well-formed) XML Doc?

爱⌒轻易说出口 提交于 2019-12-01 17:18:22
I'm creating an XML document: I want to unit test at least to make sure it's well-formed. So far, I have only been able to approximate this , by using the 'hasElements' in the REXML library. Is there a better way ? Preferably using built-in libraries (I mean libraries that ship with the standard Ruby 1.8.x distro). require "test/unit" require 'rexml/document' require 'test/unit/ui/console/testrunner' include REXML class TestBasic < Test::Unit::TestCase def test_createXML my_xml=...create doc here... doc = Document.new(my_xml); assert(doc.has_elements?); end end Test::Unit::UI::Console:

Ruby convert single quotes to double quotes in XML

南楼画角 提交于 2019-12-01 03:24:49
Despite the fact that XML attributs can be defined using single or double quotes, my user is trying to integrate my software with another one that will not accept single quoted attribut values. I user REXML to generate my XMLs. Is there a way to REXML generate double quoted attribute values? If not, is there a way for me to convert it easily? Thanks As of Feb 2007 there's a supported way of determining the quoting character. The changes were merged into Ruby sources on Jul 2007 and should be available on all versions since 1.8.6-p110: require 'rexml/document' doc = REXML::Document.new doc