CS639 Using DOM to create and read Documents with Namespaces

 

This program and SimpleSVG.java and DOMReader.java are available in $cs639/dom

 

// Example 10.5, simplified to not produce a DOCTYPE

// This version uses namespace prefixes. See SimpleSVG.java

// for case using a default namespace

// Output, with added newlines:

//  <?xml version="1.0" encoding="UTF-8" standalone="no"?>

//  <!--An example from Chapter 10 of Processing XML with Java-->

//  <?xml-stylesheet type="text/css" href="standard.css"?>

//    <s:svg xmlns:s="http://www.w3.org/2000/svg">

//       <s:desc>An example from Processing XML with Java</s:desc></s:svg>

import javax.xml.parsers.*;

import javax.xml.transform.*;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.*;

import java.io.IOException;

 

 

public class SimpleSVG1 {

 

  public static void main(String[] args) {

 

    try {

      // Find the implementation

      DocumentBuilderFactory factory

       = DocumentBuilderFactory.newInstance();

      factory.setNamespaceAware(true);

      DocumentBuilder builder = factory.newDocumentBuilder();

      DOMImplementation impl = builder.getDOMImplementation();

 

      // Create the document type node (not required)

      //      DocumentType svgDOCTYPE = impl.createDocumentType(

      // "svg", "-//W3C//DTD SVG 1.0//EN",

      // "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"

      // );

      Document doc = impl.createDocument(

         "http://www.w3.org/2000/svg", "s:svg", null); //added s:

 

      // Fill the document

      Node rootElement = doc.getDocumentElement();

      ProcessingInstruction xmlstylesheet

       = doc.createProcessingInstruction("xml-stylesheet",

       "type=\"text/css\" href=\"standard.css\"");

      Comment comment = doc.createComment(

       "An example from Chapter 10 of Processing XML with Java");

      doc.insertBefore(comment, rootElement);

      doc.insertBefore(xmlstylesheet, rootElement);

      Node desc = doc.createElementNS(

              "http://www.w3.org/2000/svg", "s:desc");  //added s:

      rootElement.appendChild(desc);

      Text descText = doc.createTextNode(

       "An example from Processing XML with Java");

      desc.appendChild(descText);

      // Serialize the document onto System.out

      TransformerFactory xformFactory

       = TransformerFactory.newInstance();

      Transformer idTransform = xformFactory.newTransformer();

      Source input = new DOMSource(doc);

      Result output = new StreamResult(System.out);

      idTransform.transform(input, output);

 

    }

    catch (FactoryConfigurationError e) {

      System.out.println("Could not locate a JAXP factory class");

    }

    catch (ParserConfigurationException e) {

      System.out.println(

        "Could not locate a JAXP DocumentBuilder class"

      );

    }

    catch (DOMException e) {

      System.err.println(e);

    }

    catch (TransformerConfigurationException e) {

      System.err.println(e);

    }

    catch (TransformerException e) {

      System.err.println(e);

    }

  }

}

// Read output from SimpleSVG1.java, i.e., XML with namespace prefixes

// Input in file svg.xml, with added newlines:

//  <?xml version="1.0" encoding="UTF-8" standalone="no"?>

//  <!--An example from Chapter 10 of Processing XML with Java-->

//  <?xml-stylesheet type="text/css" href="standard.css"?>

//    <s:svg xmlns:s="http://www.w3.org/2000/svg">

//       <s:desc>An example from Processing XML with Java</s:desc></s:svg>

 

import org.w3c.dom.*;

import javax.xml.parsers.*;

import java.io.File;

 

public class DOMReader {

    public static void main(String[] args) {

        try {

            DocumentBuilderFactory builderFactory

                = DocumentBuilderFactory.newInstance();

            builderFactory.setCoalescing(true);

            builderFactory.setNamespaceAware(true);

            DocumentBuilder builder

                = builderFactory.newDocumentBuilder();

            Document response = builder.parse(new File("svg.xml"));

            String nsUri = response.getDocumentElement().getNamespaceURI();

            NodeList descs = response.getElementsByTagNameNS(nsUri, "desc");

            Node datum = descs.item(0);

            Text result = (Text) datum.getFirstChild();

            System.out.println("contents of desc: " +result.getNodeValue());

        }

        catch (Exception e)

            {

                System.out.println("exception: " + e);

            }

    }

}

 

java DOMReader

contents of desc: An example from Processing XML with Java