Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

June 28, 2010

Create and print a simple XML document in Java using javax.xml.transform.Transformer


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;
}
}

March 30, 2010

XML namespaces intro

XML:
Simple example without schema involvement
FILL IN

XML Schema:
targetNamespace

"targetNamespace is used in a schema to specify the namespace that the schema is intended to define" -David Peterson, at JavaRanch

"Each schema document has exactly one target namespace." -XML in a Nutshell

default namespace

noNamespaceSchemaLocation
noNamespaceSchemaLocation: schema to reference for elements that have no namespace prefix?
"The noNamespaceSchemaLocation attribute references an XML Schema document that does not have a target namespace. "

schemaLocation

Interface between XML document and its schema:
FILL IN

Note from w3schools:
The namespace URI is not used by the parser to look up information.

The purpose is to give the namespace a unique name. However, often companies use the namespace as a pointer to a web page containing namespace information.

March 5, 2010

XML targetNamespace

"When we want to [validate an XML doc], we need to identify which element and attribute declarations and type definitions in the schemas should be used to check which elements and attributes in the instance document. The target namespace plays an important role in the identification process."
-from w3c here

Qualified and qualified elements are discussed on this page under the section "Qualified or Unqualified."

Qualified: "all the elements and attributes in the instance must have a namespace, which ... adds namespace complexity"
Unqualified: "only the globally declared elements and attributes in the instance must have a namespace, which ... hides the namespace complexity from the instance"

The meaning of global is described on this page:

"When an element declaration is a child of the xs:schema element, the declared element is global. Global elements can be referenced by other element declarations, allowing for element reuse."

More on global vs. local here.

March 2, 2010

Validating XML against schemas in Java

XML validation with namespaces will not work properly unless the DocumentBuilderFactory has namespace awareness turned on manually, via setNamespaceAware.

January 25, 2010

Intro to XPath

Helpful XPath syntax reference from W3

Sample code based loosely on this Java2S example:

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XMLParserAlpha
{
private static final String SOURCE = "C:/someFolder/xmlFile.xml";
private static final String EXPR = "*";

public static void main(String[] args)
{
XPath xpath = XPathFactory.newInstance().newXPath();
String xpathExpression = EXPR;
InputSource inputSource = new InputSource(SOURCE);

NodeList nodes = null;
int j = -1;
try
{
nodes = (NodeList) xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET);
// NODESET maps to interface org.w3c.dom.NodeList
j = nodes.getLength();

for (int i = 0; i < j; i++) { System.out.println(nodes.item(i).getTextContent()); } } catch (XPathExpressionException e) { e.printStackTrace(); } } }


Other values of EXPR and what they're good for:
/Report/PublicationMetadata/DescriptiveMetadata/Title/@* - All attribute values of the title element (but not the attribute names)

TODO: FINISH POST

January 13, 2010

Intro to working with XML using Java

Rob Lybarger wrote a great intro article about this, the link skips to page two of five: Working with XML and Java