Skip to content

xmlToJson with single quoted attributes #841

@DmitryIsakbayev

Description

@DmitryIsakbayev

xmlToJson used to support both double and single quoted attributes. The change happened in 1.92

Added to the test xml

   <minus a='complex'>
     element as well
   </minus>

Test with 1.91

java --class-path underscore-1.91.jar:. ToJsonTest
<mydocument has="an attribute">
   <and>
   <many>elements</many>
    <many>more elements</many>
   </and>
   <plus a="complex">
     element as well
   </plus>
   <minus a='complex'>
     element as well
   </minus>
</mydocument>
{
  "mydocument": {
    "-has": "an attribute",
    "and": {
      "many": [
        "elements",
        "more elements"
      ]
    },
    "plus": {
      "-a": "complex",
      "#text": "\n     element as well\n   "
    },
    "minus": {
      "-a": "complex",
      "#text": "\n     element as well\n   "
    }
  },
  "#omit-xml-declaration": "yes"
}

Test with 1.92

java --class-path underscore-1.92.jar:. ToJsonTest
<mydocument has="an attribute">
   <and>
   <many>elements</many>
    <many>more elements</many>
   </and>
   <plus a="complex">
     element as well
   </plus>
   <minus a='complex'>
     element as well
   </minus>
</mydocument>
{
  "mydocument": {
    "-has": "an attribute",
    "and": {
      "many": [
        "elements",
        "more elements"
      ]
    },
    "plus": {
      "-a": "complex",
      "#text": "\n     element as well\n   "
    },
    "minus": "\n     element as well\n   "
  },
  "#omit-xml-declaration": "yes"
}

Using

public class ToJsonTest {
    public static void main(String[] args) {
        
        final String xml =
          "<mydocument has=\"an attribute\">\n"
              + "   <and>\n"
              + "   <many>elements</many>\n"
              + "    <many>more elements</many>\n"
              + "   </and>\n"
              + "   <plus a=\"complex\">\n"
              + "     element as well\n"
              + "   </plus>\n"
              + "   <minus a='complex'>\n"
              + "     element as well\n"
              + "   </minus>\n"
              + "</mydocument>";

      System.out.println(xml);

      System.out.println(com.github.underscore.U.xmlToJson(xml));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions