1. XStream
  2. XSTR-526

Parsing fails when XML element has attribute and element with same name


    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Not A Bug
    • Affects Version/s: 1.3
    • Fix Version/s: None
    • Component/s: Converters
    • Labels:


      Long time lurker, first time poster...

      I've got no control over the XML I'm parsing, and unfortunately I have a problem with it. Basically, the XML has an element that has the same name for an attribute and a child element, and the inner element has an attribute of the same name. The inner element is not a problem, but mentioned just because it's also stoopid, and I felt the example warranted some data for the inner element...

      <parseme stoopid="attribute here">
      <stoopid stoopid="also attribute />

      I've got simple JUnit test class to help show my trouble (imports and get/setters truncated for brevity). File is attached with imports and all. The code compiles and runs (with the getters/setters added and the right imports).

      public class StoopidTest extends TestCase {
      public class ParseMe

      { private String stoopidAttribute = null; private Stoopid stoopidElement = null; }

      public class Stoopid

      { private String stoopid = null; }

      public void testXStream() throws Exception

      { XStream xstream = new XStream(); xstream.alias("parseme", ParseMe.class); xstream.alias("stoopid", Stoopid.class); xstream.aliasAttribute(ParseMe.class, "stoopidAttribute", "stoopid"); xstream.aliasField("stoopid", ParseMe.class, "stoopidElement"); xstream.useAttributeFor(Stoopid.class, "stoopid"); ParseMe parseMe = (ParseMe)xstream .fromXML("<parseme stoopid=\"attribute here\"><stoopid stoopid=\"also attribute\" /></parseme>"); System.out.println(xstream.toXML(parseMe)); Stoopid stoopid = parseMe.getStoopidElement(); assertEquals("also attribute", stoopid.getStoopid()); assertEquals("attribute here", parseMe.getStoopidAttribute()); }

      public void testDOM() throws Exception {
      InputStream inputStream = new ByteArrayInputStream(
      "<parseme stoopid=\"attribute here\"><stoopid stoopid=\"also attribute\" /></parseme>"
      DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
      Document document = documentBuilder.parse(inputStream);

      Node parseMe = document.getDocumentElement();
      assertEquals("parseme", parseMe.getNodeName());

      assertEquals("attribute here", parseMe.getAttributes().getNamedItem(

      Node stoopid = null;
      NodeList nodeList = parseMe.getChildNodes();
      for (int i = nodeList.getLength(); stoopid == null && i > 0; i--)

      { stoopid = nodeList.item(i - 1); if (stoopid != null && !"stoopid".equals(stoopid.getNodeName())) stoopid = null; }

      assertEquals("also attribute", stoopid.getAttributes().getNamedItem(

      In the testXTream method, the last assert (for "attribute here") fails with null as the stoopidAttribute value. The println prints the following, showing that the attribute is indeed not filled (as does running through with the debugger, but I can't as easily show that in e-mail):

      <stoopid stoopid="also attribute"/>

      And for those that will no doubt try to tell me to rearrange the getters, if I move the assert for getStoopidAttribute() before the tests for the Stoopid element, it still fails on that line, so the element is really null. Also, it fails the same using the XPP3 and DomDriver; I've not tried any others.

      I suspect there's a problem with the attribute and field having the same alias. In fact, if I reverse the aliasAttribute() and aliasField() for the ParseMe.class (to alias the field first), I get a DuplicateFieldException on stoopidAttribute. It must then be the case that the one supercedes the other; inconsistent that one throws an exception and the other doesn't, but that's just a notice, probably not my problem.

      I can read it directly using DOM, as the second test shows (it passes), but if I was going to do that, I wouldn't be trying to use XStream, would I? I'd prefer to use XStream.

      I'm curious what might be done to correct this. Any tips are welcome.



        • Assignee:
          Jörg Schaible
          Jeff Warren
        • Votes:
          0 Vote for this issue
          0 Start watching this issue


          • Created: