|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectdd.util.XMLHelper
Utility class for parsing XML. This class provides some convenience functions for creating and using a DOM tree produced by parsing an XML source file.
This class provides a simple syntax for returning a node from a
structured XML document. A path to a node is a series
of nested tags, separated by periods, such as
"outer.middle.inner
". You can specify a path from the
root or any subpath. The search routine searches the tree recursively
from the beginning to the end of the file and returns the first
node that matches the path specified. Normally that node contains text
that could then be used for something, but you can also retrieve any
node which will contain the target node and and sub-nodes.
Consider the following XML file:
<?xml version="1.0" encoding="UTF-8"?> <order> <item> <book> <title>War and Peace</title> </book> </item> <item> <magazine> <title>Wired</title> <price>19.95</price> </magazine> </item> <item> <cd> <title>Best of Leo Kottke</title> </cd> </item> </order>
Here would be the result of various queries:
Query | Result |
---|---|
order.item.magazine.title | Wired |
cd.title | Best of Leo Kottke |
order.book | null |
item.cd | Node:
<title>Best of Leo Kottke</title> |
title | War and Peace |
Example usage:
Document myDocument = XMLHelper.makeDocument("myFile.xml"); double price = XMLHelper.findDouble(myDocument, "magazine.price"); String cdTitle = XMLHelper.findString(myDocument, "order.item.cd.title");
Constructor Summary | |
XMLHelper()
|
Method Summary | |
protected static java.lang.String[] |
convertQueryToArray(java.lang.String query)
Compiles a query for internal representation. |
static org.w3c.dom.Element |
createNode(org.w3c.dom.Document document,
java.lang.String nodeName)
Creates a new empty XML node. |
static org.w3c.dom.Element |
createNode(org.w3c.dom.Document document,
java.lang.String nodeName,
org.w3c.dom.Node parentNode)
Creates a new node and inserts it into the DOM tree. |
static org.w3c.dom.Element |
createNode(org.w3c.dom.Document document,
java.lang.String nodeName,
java.lang.String attributeName,
java.lang.String attributeValue,
org.w3c.dom.Node parentNode)
Creates a new node and inserts it into the DOM tree. |
static org.w3c.dom.Element |
createTextNode(org.w3c.dom.Document document,
java.lang.String nodeName,
java.lang.String nodeText,
org.w3c.dom.Node parentNode)
Creates a new text node and interts it into the DOM tree. |
static org.w3c.dom.Node |
findChild(org.w3c.dom.Node n,
java.lang.String elementName)
Queries the immediate children of a node. |
static double |
findDouble(org.w3c.dom.Document d,
java.lang.String target)
Returns the matching node as a double. |
static org.w3c.dom.Element |
findFirstElement(org.w3c.dom.Node n)
Returns the first child element of the specified node. |
static int |
findInt(org.w3c.dom.Document d,
java.lang.String target)
Returns the matching node as an integer. |
static org.w3c.dom.Node |
findNode(org.w3c.dom.Document d,
java.lang.String target)
Finds a node matching the specified path. |
protected static org.w3c.dom.Node |
findNode(org.w3c.dom.Node n,
java.lang.String[] targets,
int index)
Finds the node recursively. |
static java.lang.String |
findString(org.w3c.dom.Document d,
java.lang.String target)
Returns the matching node as a String. |
static java.lang.String |
getNodeAttribute(org.w3c.dom.Node node,
java.lang.String attributeName)
Access the attributes of a node. |
static void |
main(java.lang.String[] args)
Runs the XMLHelper. |
static org.w3c.dom.Document |
makeDocument()
Creates a new empty XML document. |
static org.w3c.dom.Document |
makeDocument(java.io.Reader reader)
Creates a new parsed XML document from a reader. |
static org.w3c.dom.Document |
makeDocument(java.lang.String fileName)
Creates a new parsed XML document from a file. |
static java.lang.String |
nodeText(org.w3c.dom.Node n)
Returns the string contents of a node. |
static java.lang.StringBuffer |
nodeText(org.w3c.dom.Node n,
java.lang.StringBuffer text)
Returns the string contents of a node. |
static double |
parseXMLDouble(java.lang.String s)
Converts the String into a double. |
static int |
parseXMLInt(java.lang.String s)
Converts the String into an integer. |
static void |
printTree(org.w3c.dom.Node n,
int lvl)
Print the tree representing a node in the XML document. |
static void |
writeXMLFile(java.lang.String fileName,
org.w3c.dom.Node mainNode)
Writes a new XML file from the current node. |
static void |
writeXMLFile(java.io.Writer out,
org.w3c.dom.Node mainNode)
Writes the given XML node to the writer provided. |
static java.lang.String |
writeXMLString(org.w3c.dom.Node mainNode)
Writes the given node out as a String. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public XMLHelper()
Method Detail |
protected static java.lang.String[] convertQueryToArray(java.lang.String query)
query
- the incoming dot-path query
protected static org.w3c.dom.Node findNode(org.w3c.dom.Node n, java.lang.String[] targets, int index)
null
if not found.
n
- the node to begin the searchtargets
- the expanded query stringindex
- level into the query string of the recursive search
null
if
not found.findNode(Document,String)
public static org.w3c.dom.Node findChild(org.w3c.dom.Node n, java.lang.String elementName)
n
- parent node under which search takes placeelementName
- name of the node to find
elementName
,
null
if there is no child node with the name specified.public static org.w3c.dom.Element findFirstElement(org.w3c.dom.Node n)
n
- parent node under which child element should be found
null
if there are no
child element nodes.public static org.w3c.dom.Node findNode(org.w3c.dom.Document d, java.lang.String target)
d
- document to searchtarget
- the path of the node to be located
null
if
not found.public static java.lang.String findString(org.w3c.dom.Document d, java.lang.String target)
d
- document to searchtarget
- the path of the node to be located
null
if not
found.public static double findDouble(org.w3c.dom.Document d, java.lang.String target)
<target>10.2</target>
.
d
- document to searchtarget
- the path of the node to be located
-1.0
if not
found or non-numeric value.public static int findInt(org.w3c.dom.Document d, java.lang.String target)
<target>10</target>
.
d
- document to searchtarget
- the path of the node to be located
-1
if not
found or non-numeric value.public static java.lang.String getNodeAttribute(org.w3c.dom.Node node, java.lang.String attributeName)
<item price="50"/>
, when queried for "price"
will return "50".
node
- node which contains the attributeattributeName
- name of the attribute to query
null
if it does not
exist.public static double parseXMLDouble(java.lang.String s)
s
- the string to parse into a double
Double.parseDouble(String)
public static int parseXMLInt(java.lang.String s)
s
- the string to parse into a double
Integer.parseInt(String)
public static org.w3c.dom.Document makeDocument()
public static org.w3c.dom.Document makeDocument(java.io.Reader reader)
reader
- character input stream from which XML is read
null
if
an error occurs in parsing. Errors are printed to standard error.public static org.w3c.dom.Document makeDocument(java.lang.String fileName)
fileName
- name of the XML file to read
null
if
an error occurs in parsing. Errors are printed to standard error.public static void writeXMLFile(java.lang.String fileName, org.w3c.dom.Node mainNode)
fileName
- name of the XML file to createmainNode
- root of the DOM tree to write to the filepublic static java.lang.String writeXMLString(org.w3c.dom.Node mainNode)
mainNode
- root of the DOM tree to write
public static void writeXMLFile(java.io.Writer out, org.w3c.dom.Node mainNode)
out
- output writer onto which XML should be writtenmainNode
- root of the DOM tree to writepublic static java.lang.String nodeText(org.w3c.dom.Node n)
n
- current node
public static java.lang.StringBuffer nodeText(org.w3c.dom.Node n, java.lang.StringBuffer text)
n
- current nodetext
- text accumulated so far
public static void printTree(org.w3c.dom.Node n, int lvl)
n
- current nodelvl
- index for the recursive parsepublic static org.w3c.dom.Element createNode(org.w3c.dom.Document document, java.lang.String nodeName)
document
- XML document into which the node will be insertednodeName
- name of the XML tag
public static org.w3c.dom.Element createNode(org.w3c.dom.Document document, java.lang.String nodeName, org.w3c.dom.Node parentNode)
parentNode
.
document
- XML document into which the node will be insertednodeName
- name of the XML tagparentNode
- node to which this node will be added as a child
public static org.w3c.dom.Element createNode(org.w3c.dom.Document document, java.lang.String nodeName, java.lang.String attributeName, java.lang.String attributeValue, org.w3c.dom.Node parentNode)
nodeName
is
"my-node"
, attributeName
is
"length"
, and attributeValue
is
"seven"
, this method creates a new XML node that like
<my-node length="seven"/>
If
attributeName
or attributeValue
are
null
, then the name/value pair is ommited. The new node
is inserted into the DOM tree as a child of parentNode
.
The newly created node is returned in case further manipulation is
desired.
document
- XML document into which the node will be insertednodeName
- name of the XML tagattributeName
- optional key to add to XML tagattributeValue
- optional value to be associated with keyparentNode
- node to which this node will be added as a child
public static org.w3c.dom.Element createTextNode(org.w3c.dom.Document document, java.lang.String nodeName, java.lang.String nodeText, org.w3c.dom.Node parentNode)
nodeName
,
which is added to the DOM tree as a child of parentNode
.
Any XML escaping of the string is done automatically by this method.
document
- XML document into which the node will be insertednodeName
- name of the XML tagenodeText
- text which will be added to the new nodeparentNode
- node to which this node will be added as a child
public static void main(java.lang.String[] args)
args
- command line arguments
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |