Gary L Peskin wrote:
> Martin --
>
> The DOM code looks fine. I read it just the opposite of how you created
> it:
>
> FileInputStream istream = new
> FileInputStream("c:\\temp\\domMartin.ser");
> ObjectInputStream p = new ObjectInputStream(istream);
> Document doc = (Document) p.readObject();
> istream.close();
I see - somehow I didn't think so far... ;-)
>
> I think you should send the entire java program that you're using. You
> show the call to transform but not how the transformer itself was
> created so I think you should send the entire java program.
The sourcecode is attached to this mail. (It's based on the
DOM2DOM-sample of the Xalan2-distribution. I wanted to try out working
with selfbuilded DOMs there before going with it to my real project). I
also attached a file with the output (containing a few lines of
debugging-stuff and then the stacktrace of the thrown exception(s)) of
that program.
I just recognized another interesting thing: When I put calling the
transformation into a try-catch-block with ignoring the exception, then
he does a transformation, but only for the first appearance of the node
the template should match with. Maybe the prg does that once and
terminates then with the exception...
> Gary
Thanks
Martin
// Imported TraX classes
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
// Imported java.io classes
import java.io.IOException;
import java.io.FileNotFoundException;
// Imported SAX classes
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
// Imported DOM classes
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
// Imported Serializer classes
import org.apache.xalan.serialize.Serializer;
import org.apache.xalan.serialize.SerializerFactory;
import org.apache.xalan.templates.OutputProperties;
// Imported JAVA API for XML Parsing classes
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Element;
import org.apache.xerces.dom.DocumentImpl;
import javax.xml.transform.ErrorListener;
/**
* Show how to transform a DOM tree into another DOM tree.
* This uses the javax.xml.parsers to parse both an XSL file
* and the XML file into a DOM, and create an output DOM.
*/
public class myDOM2DOM
{
private static final String NAMESPACE = "http://www.w3.org/1999/XSL/Transform";
// private static final String NAMESPACE = "http://mein.toller.namespace";
public static void main(String[] args)
throws TransformerException, TransformerConfigurationException,
FileNotFoundException, ParserConfigurationException,
SAXException, IOException
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Element tempElement = null;
if(tFactory.getFeature(DOMSource.FEATURE) &&
tFactory.getFeature(DOMResult.FEATURE))
{
Document xslDoc = null;
try {
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
dFactory.setNamespaceAware( true );
DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
// With this, all works very well (see xalan-sample "DOM2DOM"):
// // Use the DocumentBuilder to parse the XSL stylesheet.
// xslDoc = dBuilder.parse("birds.xsl");
DOMImplementation domImpl = dBuilder.getDOMImplementation();
xslDoc = domImpl.createDocument( NAMESPACE, "xsl:stylesheet", null );
tempElement = xslDoc.getDocumentElement();
System.out.println( "DocumentElement: " + tempElement.getTagName() );
tempElement.setAttributeNS( null, "version", "1.0" );
tempElement.setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns:xsl",
"http://www.w3.org/1999/XSL/Transform" );
Element output = (Element) xslDoc.createElementNS( NAMESPACE, "xsl:output" );
output.setAttributeNS( null, "method", "xml");
output.setAttributeNS( null, "indent", "yes");
tempElement.appendChild( output );
Element el1 = (Element) xslDoc.createElementNS( NAMESPACE, "xsl:template" );
el1.setAttributeNS( null, "match", "Order" );
Element el2 = (Element) xslDoc.createElementNS( null, "hr" );
el1.appendChild( el2 );
tempElement.appendChild( el1 );
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
System.out.println( "Tatah...! - ParserConfigurationException" );
pce.printStackTrace();
}
/*
java.io.FileOutputStream fos = new java.io.FileOutputStream("dom.ser");
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(fos);
oos.writeObject(xslDoc);
oos.flush();
fos.close();
*/
System.out.println( "--------------------------------------------" );
Serializer serializer = SerializerFactory.getSerializer
(OutputProperties.getDefaultMethodProperties("xml"));
serializer.setOutputStream(System.out);
serializer.asDOMSerializer().serialize( xslDoc);
System.out.println( "\n--------------------------------------------" );
// Use the DOM Document to define a DOMSource object.
DOMSource xslDomSource = new DOMSource();
System.out.println("Jetzt Zuweisung der Node zum DomSource");
xslDomSource.setNode( xslDoc );
System.out.println( "getNode: " + xslDomSource.getNode().getNodeName() );
// Set the systemId: note this is actually a URL, not a local filename
xslDomSource.setSystemId( "myXSLT.xsl" ); // "http://blah.blub");
// Process the stylesheet DOMSource and generate a Transformer.
System.out.println("bis hierher bin ich gekommen...");
Transformer transformer = tFactory.newTransformer(xslDomSource);
System.out.println("und wie stehts mit dieser Codestelle?");
//Use the DocumentBuilder to parse the XML input.
Document xmlDoc = null;
try {
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
// And setNamespaceAware, which is required when parsing xsl files
dFactory.setNamespaceAware(true);
//Use the DocumentBuilderFactory to create a DocumentBuilder.
DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
System.out.println("Parse nun birds.xml");
xmlDoc = dBuilder.parse("birds.xml");
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
System.out.println( "Tatah...! - ParserConfigurationException" );
pce.printStackTrace();
}
// Use the DOM Document to define a DOMSource object.
DOMSource xmlDomSource = new DOMSource(xmlDoc);
// Set the base URI for the DOMSource so any relative URIs it contains can
// be resolved.
xmlDomSource.setSystemId("birds.xml");
// Create an empty DOMResult for the Result.
DOMResult domResult = new DOMResult();
System.out.println("Rufe nun den Transformer auf.");
// Perform the transformation, placing the output in the DOMResult.
// try{
transformer.transform(xmlDomSource, domResult); // <- !!!
// }catch( javax.xml.transform.TransformerException te ) {
// ...
// }
// Instantiate an Xalan XML serializer and use it to serialize the
// output DOM to System.out.
// using a default output format.
// Serializer serializer = SerializerFactory.getSerializer
//
(OutputProperties.getDefaultMethodProperties("xml"));
// serializer.setOutputStream(System.out);
serializer.asDOMSerializer().serialize(domResult.getNode());
}
else
{
throw new org.xml.sax.SAXNotSupportedException("DOM node processing not
supported!");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Class>
<Order Name="TINAMIFORMES">
<Family Name="TINAMIDAE">
<Species Scientific_Name="Tinamus major"> Great Tinamou.</Species>
<Species Scientific_Name="Nothocercus">Highland Tinamou.</Species>
<Species Scientific_Name="Crypturellus soui">Little Tinamou.</Species>
<Species Scientific_Name="Crypturellus cinnamomeus">Thicket Tinamou.</Species>
<Species Scientific_Name="Crypturellus boucardi">Slaty-breasted Tinamou.</Species>
<Species Scientific_Name="Crypturellus kerriae">Choco Tinamou.</Species>
</Family>
</Order>
<Order Name="GAVIIFORMES">
<Family Name="GAVIIDAE">
<Species Scientific_Name="Gavia stellata">Red-throated Loon.</Species>
<Species Scientific_Name="Gavia arctica">Arctic Loon.</Species>
<Species Scientific_Name="Gavia pacifica">Pacific Loon.</Species>
<Species Scientific_Name="Gavia immer">Common Loon.</Species>
<Species Scientific_Name="Gavia adamsii">Yellow-billed Loon.</Species>
</Family>
</Order>
<Order Name="PODICIPEDIFORMES">
<Family Name="PODICIPEDIDAE">
<Species Scientific_Name="Tachybaptus dominicus">Least Grebe.</Species>
<Species Scientific_Name="Podilymbus podiceps">Pied-billed Grebe.</Species>
<Species Scientific_Name="">Atitlan Grebe.</Species>
<Species Scientific_Name="">Horned Grebe.</Species>
<Species Scientific_Name="">Red-necked Grebe.</Species>
<Species Scientific_Name="">Eared Grebe.</Species>
<Species Scientific_Name="">Western Grebe.</Species>
<Species Scientific_Name="">Clark's Grebe.</Species>
<Species Scientific_Name=""/>
</Family>
</Order>
<Order Name="PROCELLARIIFORMES">
<Family Name="DIOMEDEIDAE">
<Species Scientific_Name="Thalassarche chlororhynchos">Yellow-nosed Albatross. (A)</Species>
<Species Scientific_Name="Thalassarche cauta">Shy Albatross. (A)</Species>
<Species Scientific_Name="Thalassarche melanophris">Black-browed Albatross. (A)</Species>
<Species Scientific_Name="Phoebetria palpebrata">Light-mantled Albatross. (A)</Species>
<Species Scientific_Name="Diomedea exulans">Wandering Albatross. (A)</Species>
<Species Scientific_Name="Phoebastria immutabilis">Laysan Albatross.</Species>
<Species Scientific_Name="Phoebastria nigripes">Black-footed Albatross.</Species>
<Species Scientific_Name="Phoebastria albatrus">Short-tailed Albatross. (N)</Species>
</Family>
<Family Name="PROCELLARIIDAE">
<Species Scientific_Name="Fulmarus glacialis">Northern Fulmar.</Species>
<Species Scientific_Name="Pterodroma neglecta">Kermadec Petrel. (A)</Species>
<Species Scientific_Name="Pterodroma arminjoniana">Herald Petrel. (A)</Species>
<Species Scientific_Name="Pterodroma ultima">Murphy's Petrel. (N)</Species>
<Species Scientific_Name="Pterodroma inexpectata">Mottled Petrel. (A)</Species>
<Species Scientific_Name="Pterodroma cahow">Bermuda Petrel.</Species>
<Species Scientific_Name="Pterodroma hasitata">Black-capped Petrel.</Species>
<Species Scientific_Name="Pterodroma externa">Juan Fernandez Petrel. (N)</Species>
<Species Scientific_Name="Pterodroma phaeopygia">Dark-rumped Petrel.</Species>
<Species Scientific_Name="Pterodroma cervicalis">White-necked Petrel. (H)</Species>
<Species Scientific_Name="Pterodroma hypoleuca">Bonin Petrel. (H)</Species>
<Species Scientific_Name="Pterodroma nigripennis">Black-winged Petrel. (H, A)</Species>
<Species Scientific_Name="Pterodroma cookii">Cook's Petrel. (N)</Species>
<Species Scientific_Name="Pterodroma longirostris">Stejneger's Petrel. (A)</Species>
<Species Scientific_Name="Bulweria bulwerii">Bulwer's Petrel. (H)</Species>
<Species Scientific_Name="Bulweria fallax">Jouanin's Petrel. (H, A)</Species>
<Species Scientific_Name="Procellaria parkinsoni">Parkinson's Petrel. (N)</Species>
<Species Scientific_Name="Calonectris leucomelas">Streaked Shearwater. (A)</Species>
<Species Scientific_Name="Calonectris diomedea">Cory's Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus creatopus">Pink-footed Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus carneipes">Flesh-footed Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus gravis">Greater Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus pacificus">Wedge-tailed Shearwater.</Species>
<Species Scientific_Name="Puffinus bulleri">Buller's Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus griseus">Sooty Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus tenuirostris">Short-tailed Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus nativitatis">Christmas Shearwater. (H)</Species>
<Species Scientific_Name="Puffinus puffinus">Manx Shearwater.</Species>
<Species Scientific_Name="Puffinus auricularis">Townsend's Shearwater.</Species>
<Species Scientific_Name="Puffinus opisthomelas">Black-vented Shearwater.</Species>
<Species Scientific_Name="Puffinus lherminieri">Audubon's Shearwater.</Species>
<Species Scientific_Name="Puffinus assimilis">Little Shearwater. (A)</Species>
</Family>
<Family Name="HYDROBATIDAE">
<Species Scientific_Name="Oceanites oceanicus">Wilson's Storm-Petrel. (N)</Species>
<Species Scientific_Name="Pelagodroma marina">White-faced Storm-Petrel. (A)</Species>
<Species Scientific_Name="Hydrobates pelagicus">European Storm-Petrel. (A)</Species>
<Species Scientific_Name="Oceanodroma furcata">Fork-tailed Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma leucorhoa">Leach's Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma homochroa">Ashy Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma castro">Band-rumped Storm-Petrel. (N)</Species>
<Species Scientific_Name="Oceanodroma tethys">Wedge-rumped Storm-Petrel. (N)</Species>
<Species Scientific_Name="Oceanodroma melania">Black Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma macrodactyla">Guadalupe Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma markhami">Markham's Storm-Petrel. (A)</Species>
<Species Scientific_Name="Oceanodroma tristrami">Tristram's Storm-Petrel. (H)</Species>
<Species Scientific_Name="Oceanodroma microsoma">Least Storm-Petrel.</Species>
</Family>
</Order>
<Order Name="PELECANIFORMES">
<Family Name="PHAETHONTIDAE">
<Species Scientific_Name="Phaethon lepturus">White-tailed Tropicbird.</Species>
<Species Scientific_Name="Phaethon aethereus">Red-billed Tropicbird.</Species>
<Species Scientific_Name="Phaethon rubricauda">Red-tailed Tropicbird.</Species>
</Family>
<Family Name="SULIDAE">
<Species Scientific_Name="Sula dactylatra">Masked Booby.</Species>
<Species Scientific_Name="Sula nebouxii">Blue-footed Booby.</Species>
<Species Scientific_Name="Sula variegata">Peruvian Booby. (A)</Species>
<Species Scientific_Name="Sula leucogaster">Brown Booby.</Species>
<Species Scientific_Name="Sula sula">Red-footed Booby.</Species>
<Species Scientific_Name="Morus bassanus">Northern Gannet.</Species>
</Family>
<Family Name="PELECANIDAE">
<Species Scientific_Name="Pelecanus erythrorhynchos">American White Pelican.</Species>
<Species Scientific_Name="Pelecanus occidentalis">Brown Pelican.</Species>
</Family>
<Family Name="PHALACROCORACIDAE">
<Species Scientific_Name="Phalacrocorax penicillatus">Brandt's Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax brasilianus">Neotropic Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax auritus">Double-crested Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax carbo">Great Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax urile">Red-faced Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax pelagicus">Pelagic Cormorant.</Species>
</Family>
<Family Name="ANHINGIDAE">
<Species Scientific_Name="Anhinga anhinga">Anhinga.</Species>
</Family>
<Family Name="FREGATIDAE">
<Species Scientific_Name="Fregata magnificens">Magnificent Frigatebird.</Species>
<Species Scientific_Name="Fregata minor">Great Frigatebird.</Species>
<Species Scientific_Name="Fregata ariel">Lesser Frigatebird. (A)</Species>
</Family>
</Order>
</Class>
sparenbe@mango:~ > java myDOM2DOM
DocumentElement: xsl:stylesheet
--------------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output indent="yes"
method="xml"/><xsl:template match="Order"><hr/></xsl:template></xsl:stylesheet>
--------------------------------------------
Jetzt Zuweisung der Node zum DomSource
getNode: #document
bis hierher bin ich gekommen...
und wie stehts mit dieser Codestelle?
Parse nun birds.xml
Rufe nun den Transformer auf.
Exception in thread "main" javax.xml.transform.TransformerException: Can't have more
than one root on a DOM!
at
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java, Compiled
Code)
at
org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java, Compiled
Code)
at myDOM2DOM.main(myDOM2DOM.java, Compiled Code)
---------
javax.xml.transform.TransformerException: Can't have more than one root on a DOM!
at
org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java, Compiled
Code)
at
org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.java,
Compiled Code)
at
org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java,
Compiled Code)
at
org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.java,
Compiled Code)
at
org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java, Compiled
Code)
at
org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java, Compiled
Code)
at myDOM2DOM.main(myDOM2DOM.java, Compiled Code)
---------
org.xml.sax.SAXException: Can't have more than one root on a DOM!
at org.apache.xml.utils.DOMBuilder.append(DOMBuilder.java, Compiled Code)
at org.apache.xml.utils.DOMBuilder.startElement(DOMBuilder.java, Compiled Code)
at
org.apache.xalan.transformer.QueuedStartElement.flush(QueuedStartElement.java,
Compiled Code)
at
org.apache.xalan.transformer.ResultTreeHandler.flushPending(ResultTreeHandler.java,
Compiled Code)
at
org.apache.xalan.transformer.ResultTreeHandler.endElement(ResultTreeHandler.java,
Compiled Code)
at
org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java, Compiled
Code)
at
org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.java,
Compiled Code)
at
org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java,
Compiled Code)
at
org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.java,
Compiled Code)
at
org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java, Compiled
Code)
at
org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java, Compiled
Code)
at myDOM2DOM.main(myDOM2DOM.java, Compiled Code)
---------
org.xml.sax.SAXException: Can't have more than one root on a DOM!
at org.apache.xml.utils.DOMBuilder.append(DOMBuilder.java, Compiled Code)
at org.apache.xml.utils.DOMBuilder.startElement(DOMBuilder.java, Compiled Code)
at
org.apache.xalan.transformer.QueuedStartElement.flush(QueuedStartElement.java,
Compiled Code)
at
org.apache.xalan.transformer.ResultTreeHandler.flushPending(ResultTreeHandler.java,
Compiled Code)
at
org.apache.xalan.transformer.ResultTreeHandler.endElement(ResultTreeHandler.java,
Compiled Code)
at
org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java, Compiled
Code)
at
org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.java,
Compiled Code)
at
org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java,
Compiled Code)
at
org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.java,
Compiled Code)
at
org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java,
Compiled Code)
at
org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java, Compiled
Code)
at
org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java, Compiled
Code)
at myDOM2DOM.main(myDOM2DOM.java, Compiled Code)
sparenbe@mango:~ >