Transforming XSL Output to DOM
by Dinesh[ Edit ] 2012-07-23 14:11:15
Transforming XSL Output to DOM
The following example code in transforms the source XML document into a DOM object, and then passes it to a parser class to display the parsed DOM document nodes.
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.DOMResult;
public class XalanSimpleTransformToDOM
{
public static void main(String[] args)
{
XalanSimpleTransformToDOM XSTTD = new XalanSimpleTransformToDOM();
}
public XalanSimpleTransformToDOM()
{
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
String XMLSource = AmazonMacbethSpanish.xml”;
String XSLSource = XMLtoQuotes.xsl”;
DOMResult domResult = new DOMResult();
Transformer transformer = tFactory.newTransformer(new
StreamSource(XSLSource));
transformer.transform(new StreamSource(XMLSource), domResult);
nodeReader(domResult.getNode());
} catch (TransformerException e) {
private void nodeReader(Node node) {
if(node.hasChildNodes()) {
NodeList children = node.getChildNodes();
if (children != null) {
for (int i=0; i< children.getLength(); i++) {
Node ThisNode= children.item(i);
String name = children.item(i).getNodeName();
String localName = ThisNode.getLocalName();
String uri = ThisNode.getNamespaceURI();
String prefix = ThisNode.getPrefix();
String value = ThisNode.getNodeValue();
String NodeText=null;
int type = ThisNode.getNodeType();
switch (type) {
case 1: NodeText = “Node type 1: Element Constant:
ELEMENT_NODE \r\n”;break;
case 2: NodeText = “Node type 2: Attribute
Constant: ATTRIBUTE_NODE \r\n”;break;
case 3: NodeText = “Node type 3: Text Constant:
TEXT_NODE \r\n”;break;
case 4: NodeText = “Node type 4: CDATA Section
Constant: CDATA_SECTION_NODE \r\n”;break;
case 5: NodeText = “Node type 5: Entity Reference
Constant: ENTITY_REFERENCE_NODE \r\n”;break;
case 6: NodeText = “Node type 6: Entity Constant:
ENTITY_NODE \r\n”;break;
case 7: NodeText = “Node type 7: Processing
Instruction Constant: PROCESSING_INSTRUCTION_NODE
\r\n”;break;
case 8: NodeText = “Node type 8: Comment Constant:
COMMENT_NODE \r\n”;break;
case 9: NodeText = “Node type 9: Document Constant:
DOCUMENT_NODE \r\n”;break;
case 10: NodeText = “Node type 10: Document Type
Declaration Constant: DOCUMENT_TYPE_NODE
\r\n”;break;
case 11: NodeText = “Node type 11: Document
Fragment Constant: DOCUMENT_FRAGMENT_NODE
\r\n”;break;
case 12: NodeText = “Node type 12: Notation
Constant: NOTATION_NODE \r\n”;break;
//Remove this comment when DOM supports the
XPATH_NAMESPACE_NODE Constant
//case 13: NodeText = “Node type 13: W3C DOm Level
3 XPathNamespace Constant:
XPATH_NAMESPACE_NODE”;break;
default: NodeText = “Not a valid W3C Node type
\r\n”;
}
String nodeString = NodeText;
nodeString += (“Name: “ + name + “\r\n”);
if (localName != null) {
nodeString += (“Local Name: “ + localName +
“\r\n”);
}
if (prefix != null) {
nodeString += (“Prefix: “ + prefix + “\r\n”);
}
if (uri != null) {
nodeString += (“Namespace URI: “ + uri + “\r\n”);
}
if (type == ThisNode.ELEMENT_NODE) {
if (ThisNode.hasAttributes()) {
String Attributes = null;
NamedNodeMap AttributesList =
node.getAttributes();
for(int j = 0; j < AttributesList.getLength();
j++) {
nodeString +=(“Attribute Name: “ +
AttributesList.item(j).getNodeName() +
“\r\nAttribute Value: “ +
AttributesList.item(j).getNodeValue()+”\r\n” );
}
}
}
if (value != null) {
nodeString += (“Value: “ + value + “\r\n”);
}
System.out.println(nodeString);
if(children.item(i).hasChildNodes()) {
nodeReader(children.item(i));
}
}
}
}
}
}
The code in this example is almost identical to the code in the XalanSimpleTransform. A new instance of TransformerFactory is created. This time, however, the parameters for the transform method have changed. Instead of a StreamResult object, I define a DOMResult object. The DOMResult object is used to contain the transformation output instead of a Javax stream.
The nodeReader class prints out all of the nodes in a node tree to the screen.
TransformerFactory tFactory = TransformerFactory.newInstance();
String XMLSource = AmazonMacbethSpanish.xml”;
String XSLSource = XMLtoQuotes.xsl”;
System.err.println(“Error: “ + e);
}
}
DOMResult domResult = new DOMResult();
Transformer transformer = tFactory.newTransformer(new
StreamSource(XSLSource));
transformer.transform(new StreamSource(XMLSource), domResult);
nodeReader(domResult.getNode());