package pkg;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Bar
{
public static void main(String[] args)
{
List properties = new ArrayList();
properties.add(new DummyObject("n1", "t1", "v1"));
properties.add(new DummyObject("n2", "t2", "v2"));
properties.add(new DummyObject("n3", "t3", "v3"));
properties.add(new DummyObject("n4", "t4", "v4"));
properties.add(new DummyObject("n5", "t5", "v5"));
properties.add(new DummyObject("n6", "t6", "v6"));
properties.add(new DummyObject("n7", "t7", "v7"));
properties.add(new DummyObject("n8", "t8", "v8"));
properties.add(new DummyObject("n9", "t9", "v9"));
// Create XML document tree
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document documentTree = null;
Element dummyParent = null;
try
{
db = dbf.newDocumentBuilder();
documentTree = db.newDocument();
dummyParent = documentTree.createElement("DummyParent");
documentTree.appendChild(dummyParent);
for (DummyObject property : properties)
{
// Create XML tags for the property
Element propertyNode = documentTree.createElement("Property");
Element nameNode = documentTree.createElement("Name");
Element typeNode = documentTree.createElement("Type");
Element valueNode = documentTree.createElement("Value");
// Populate the property's XML tags
nameNode.setTextContent(property.getName());
typeNode.setTextContent(property.getType());
valueNode.setTextContent(property.getValue());
// Add the data to the property node
propertyNode.appendChild(nameNode);
propertyNode.appendChild(typeNode);
propertyNode.appendChild(valueNode);
// Add the property node to the XML tree
dummyParent.appendChild(propertyNode);
}
DOMSource source = new DOMSource(documentTree);
StreamResult result = new StreamResult(System.out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(source, result);
}
catch (ParserConfigurationException e)
{
System.out.println("D'oh! Creating the document failed.");
System.out.println(e.getMessage());
}
catch (TransformerException e)
{
System.out.println("D'oh! Transformer failed.");
System.out.println(e.getMessage());
}
}
}
class DummyObject
{
String name;
String type;
String value;
public DummyObject()
{
super();
}
public DummyObject(String n, String t, String v)
{
name = n;
type = t;
value = v;
}
public String getName()
{
return name;
}
public String getType()
{
return type;
}
public String getValue()
{
return value;
}
}
June 28, 2010
Create and print a simple XML document in Java using javax.xml.transform.Transformer
June 1, 2010
[Tom] Duff's Device
Duff's Device is mentioned in the JLS section about switch statements. The nitty-gritty is explained at this StackOverflow answer.
Sample code:
MyEclipse 8.5 shows two errors at the semicolon immediately before "Great C hack...":
Sample code:
package pkg;
public class Foo
{
public static void main(String args[])
{
int numTimesToCallMethod = 20;
int numTimesToRunWhile = (numTimesToCallMethod + 7) / 8;
switch (numTimesToCallMethod%8)
{
case 0:
do {
IllHaveADuffYouHaveOneToo(); // Great C hack, Tom,
case 7: IllHaveADuffYouHaveOneToo(); // but it's not valid here.
case 6: IllHaveADuffYouHaveOneToo();
case 5: IllHaveADuffYouHaveOneToo();
case 4: IllHaveADuffYouHaveOneToo();
case 3: IllHaveADuffYouHaveOneToo();
case 2: IllHaveADuffYouHaveOneToo();
case 1: IllHaveADuffYouHaveOneToo();
} while (--numTimesToRunWhile > 0);
}
}
public static void IllHaveADuffYouHaveOneToo()
{
System.out.println();
}
}
MyEclipse 8.5 shows two errors at the semicolon immediately before "Great C hack...":
Multiple markers at this line
- Syntax error, insert "while ( Expression ) ;" to complete DoStatement
- Syntax error, insert "}" to complete Block
Subscribe to:
Posts (Atom)