[FrontPage] [TitleIndex] [WordIndex

Basic handling of XML documents with XmlFragment

Loading and saving of generic XML documents

   1   // read xml fragment from a given URL
   2   XMLFragment myFrag = new XMLFragment();
   3   URL url = new URL( "file:///d:/tmp/in.xml");
   4   myFrag.load( url );
   5 
   6   // write xml fragment to a file
   7   Writer writer = new BufferedWriter(new FileWriter("d:/tmp/out.xml" ));
   8   myFrag.write(writer);
   9   writer.close();

Pretty printing

   1   // to console, for debug purposes
   2   myFrag.prettyPrint( new PrintWriter (System.out));

XSL transformation

   1   XSLTDocument sheet = new XSLTDocument();
   2   sheet.load(new URL("file:///d:/tmp/sheet.xsl"));
   3 
   4   XMLFragment input = new XMLFragment();
   5   input.load(new URL("file:///d:/tmp/in.xml"));
   6 
   7   XMLFragment result = sheet.transform(input);
   8   result.prettyPrint(new PrintWriter(System.out));

Parsing application specific XML documents

Imagine that you have an OGC specific XML document that deegree supports. Your document is a WFS Capabilities file from a remote server and your task is to extract the names of the feature types that the server offers. Instead of analyzing the structure of the document "by hand", you should cling to deegree's parser for the document format. This makes the process very easy and safe.

   1   WFSCapabilitiesDocument capabilitiesDoc = new WFSCapabilitiesDocument();
   2   capabilitiesDoc.load(new URL("file:///d:/tmp/example_wfs_capabilities.xml"));
   3   WFSCapabilities capabilities = null;
   4 
   5   try {
   6         capabilities = (WFSCapabilities) capabilitiesDoc.parseCapabilities();
   7   } catch (InvalidCapabilitiesException e) {
   8         System.out.println("Broken capabilities document: " + e.getMessage());
   9         throw (e);
  10   }
  11   FeatureTypeList featureTypeList = capabilities.getFeatureTypeList();
  12   WFSFeatureType [] featureTypes = featureTypeList.getFeatureTypes();
  13 
  14   System.out.println();
  15   for (int i = 0; i < featureTypes.length; i++) {
  16         System.out.println(featureTypes[i].getName());
  17         System.out.println(featureTypes[i].getTitle());
  18   }

Defining the TransformerFactory

If you, like me, is fed up with the Oracle Transformer interfering in the transformation, that is to say, preventing everything from working as it should, try the brute force approach to change the Transformer. To do that, you simply define a Java System Property such as:

-Djavax.xml.transform.TransformerFactory=com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl

Other Factories are

* Saxon 6.x: com.icl.saxon.TransformerFactoryImpl
* Saxon 7.x and 8.x: net.sf.saxon.TransformerFactoryImpl
* Xalan interpretive: org.apache.xalan.processor.TransformerFactoryImpl
* Xalan XSLTC: org.apache.xalan.xsltc.trax.TransformerFactoryImpl
* jd.xslt: jd.xml.xslt.trax.TransformerFactoryImpl
* Oracle: oracle.xml.jaxp.JXSAXTransformerFactory (dump it!)
* Java 1.5 bundled Xalan: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl (works for me!)


CategoryDeegree2


2018-04-20 12:04