Supporting XML 1.1 in Java 6 and Higher

This section provides a tutorial example showing that Java 6 and higher supports XML 1.1 parsing.

In order to verify changes proposed in XML 1.1, we need to use a tool that can parse XML 1.1 document. The first choice is Java, which supports XML 1.1 since Java 6 (JDK 1.6). Here is a simple program that can parse an XML 1.1 file into DOM object and print out its content in a tree structure:

/* DOMBrowser.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.   
 */
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
class DOMBrowser {
   public static void main(String[] args) {
      try {
         File x = new File(args[0]);
         DocumentBuilderFactory f
            = DocumentBuilderFactory.newInstance();
         DocumentBuilder b = f.newDocumentBuilder();
         Document d = b.parse(x);
         printNode(d, "");
      } catch (ParserConfigurationException e) {
         System.out.println(e.toString());      
      } catch (SAXException e) {
         System.out.println(e.toString());      
      } catch (IOException e) {
         System.out.println(e.toString());      
      }
   }
   static void printNode(Node n, String p) {
      NodeList l = n.getChildNodes();
      NamedNodeMap m = n.getAttributes();
      int ml = -1;
      if (m!=null) ml = m.getLength(); 
      System.out.println(p+n.getNodeName()+": "+n.getNodeType()+", "
         +l.getLength()+", "+ml+", "+n.getNodeValue());
      for (int i=0; i<ml; i++) {
         Node c = m.item(i);
         printNode(c,p+" |-");
      }
      for (int i=0; i<l.getLength(); i++) {
         Node c = l.item(i);
         printNode(c,p+" ");
      }
   }
}

To test out this example program, I compile and run it with JDK 1.8 on 3 simple XML files that have different XML version attributes:

C:\herong\xml>type hello-1-0.xml
<?xml version="1.0"?>
<p>Hello world!</p>

C:\herong\xml>java DOMBrowser hello-1-0.xml
#document: 9, 1, -1, null
 p: 1, 1, 0, null
  #text: 3, 0, -1, Hello world!

C:\herong\xml>type hello-1-1.xml
<?xml version="1.1"?>
<p>Hello world!</p>

C:\herong\xml>java DOMBrowser hello-1-1.xml
#document: 9, 1, -1, null
 p: 1, 1, 0, null
  #text: 3, 0, -1, Hello world!

C:\herong\xml>type hello-2-0.xml
<?xml version="2.0"?>
<p>Hello world!</p>

C:\herong\xml>java DOMBrowser hello-2-0.xml
[Fatal Error] hello-2-0.xml:1:20: XML version "2.0" is not supported, 
only XML 1.0 is supported.
org.xml.sax.SAXParseException; systemId: 
file:/C:/herong/xml/hello-2-0.xml; lineNumber: 1; columnNumber: 20; 
XML version "2.0" is not supported, only XML 1.0 is supported.

As you can see from the test output, Java 8 (JDK 1.8) does support XML 1.0 and XML 1.1 by default.

Last update: 2014.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

XML 1.1 Changes and Parsing Examples

 Major Changes in XML 1.1

Supporting XML 1.1 in Java 6 and Higher

 Control Codes Supported in XML 1.1

 Unicode Characters Supported in XML 1.1 Names

 End-of-Line Characters Supported in XML 1.1

 Web Browsers Not Supporting XML 1.1

 Outdated Tutorials

 References

 PDF Printing Version