Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8145963

StAX XMLEventReader incorrectly reads self-closed elements

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      Start element events read by StAX XMLEventReader do contain fixed attributes from DTD for tags with text content but there are no such attributes for self-closed tags.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect that event for self-closed tag contain attributes from DTD.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
      <!-- test.xml -->
      <?xml version="1.0" encoding="UTF-8"?>

      <!DOCTYPE top SYSTEM "test.dtd">
      <top>
          <first>1</first>
          <second/>
          <third>3</third>
      </top>

      <!-- test.dtd -->

      <?xml encoding="UTF-8"?>

      <!ELEMENT top (first, second, third)>

      <!ELEMENT first EMPTY>
      <!ATTLIST first
              a1 (val1) #FIXED "val1"
              a2 (val2) #FIXED "val2"
              %generalAttributes;>
      <!ELEMENT second EMPTY>
      <!ATTLIST second
              a1 (val1) #FIXED "val1"
              a2 (val2) #FIXED "val2"
              %generalAttributes;>

      <!ELEMENT third EMPTY>
      <!ATTLIST third
              a1 (val1) #FIXED "val1"
              a2 (val2) #FIXED "val2"
              %generalAttributes;>
      */
              package just.as.example;

              import java.io.InputStream;
              import java.util.Iterator;

              import javax.xml.stream.XMLEventReader;
              import javax.xml.stream.XMLInputFactory;
              import javax.xml.stream.XMLResolver;
              import javax.xml.stream.XMLStreamException;
              import javax.xml.stream.events.StartElement;
              import javax.xml.stream.events.XMLEvent;

              /**
               * Created by: $Author:: abekman
               * Creation time: $Date:: 2015-12-03 4:07 PM
               * Last modified by: $Author:: $
               * Modification time: $Date:: $
               * File revision: $Rev:: $
               */
              public class MainOne {

                  public static void main(String[] args) throws Exception {
                      XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
                      xmlInputFactory.setXMLResolver(new CustomResolver());
                      //xmlInputFactory.setXMLResolver(new CatalogXMLResolver());
                      try (InputStream inputStream = MainOne.class.getResourceAsStream("test.xml")) {

                          XMLEventReader reader = xmlInputFactory.createXMLEventReader(inputStream);
                          while (reader.hasNext()) {
                              XMLEvent event = reader.nextEvent();
                              if (event.isStartElement()) {
                                  StartElement startElement = event.asStartElement();
                                  System.out.println(startElement.getName().getLocalPart());
                                  Iterator iterator = startElement.getAttributes();
                                  while (iterator.hasNext()) {
                                      System.out.println("attribute " + iterator.next());
                                  }
                              }
                          }
                      }
                  }

                  private static class CustomResolver implements XMLResolver {
                      @Override
                      public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
                          return MainOne.class.getResourceAsStream(systemID);
                      }
                  }
              }

      ---------- END SOURCE ----------

        Attachments

        1. test.dtd
          0.5 kB
        2. testFile.xml
          0.1 kB
        3. TestSelfClosedXMLElements.java
          1 kB

          Activity

            People

            • Assignee:
              joehw Joe Wang
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: