1
<result>
    <sequence id="148"></sequence>
    <sequence id="211"></sequence>
    <sequence id="81">!Kyle OP Test</sequence>
    <sequence id="197">(SS) AnikSIB - 1 Hour Reminder</sequence>
    <sequence id="198">(SS) AnikSIB - 5 minutes Reminder</sequence>
<result>

How can I convert the above xml into json I want sequence id like 148 ,211,81,197 and !Kyle OP Test ,(SS) AnikSIB - 1 Hour Reminder What I am trying : Hash.from_xml(xmlresponce) And my Output is:

{
  "result": {
    "sequence": [
      {
        "id": "148"
      },
      {
        "id": "211"
      },
      "!Kyle OP Test",
      "(SS) AnikSIB - 1 Hour Reminder",
      "(SS) AnikSIB - 5 minutes Reminder",

I want the sequence id of !Kyle OP Test which I am not getting.

Elsie Hamilton
  • 155
  • 3
  • 13
  • 1
    Possible duplicate of [converting from xml name-values into simple hash](http://stackoverflow.com/questions/11139709/converting-from-xml-name-values-into-simple-hash) – lcguida Dec 20 '16 at 14:18
  • Possible duplicate of [Ruby XML to JSON Converter?](http://stackoverflow.com/questions/1530324/ruby-xml-to-json-converter) – John McGrath Apr 27 '17 at 22:36

1 Answers1

0

To deal with XML documents in professional manner, one uses nokogiri

require 'nokogiri'
doc = Nokogiri::XML(input) # XML document

doc.xpath("//sequence[text()='!Kyle OP Test']") # query for nodes with text
   .first                                       # first, since it’s unique
   .xpath("@id")                                # get attribute
   .first                                       # first, yeah
   .value                                       # value
#⇒ "81"
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160