Package nl.gx.webmanager.handler.util
Class XMLUtil
- java.lang.Object
-
- nl.gx.webmanager.handler.util.XMLUtil
-
public class XMLUtil extends Object
Various xml manipulation methods.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Document
createXmlDoc(String xml)
Creates a document for xml input.static String
doc2Xml(Document doc)
Converts a document to xml.static String
escapeToXML(String value)
Escapes special xml characters.static String
getAttribute(Node node, String attributeName)
Return the attribute with a specified name from a node.static String
getChildContents(Node node, String childName)
Return the text representation of the first child of the node with the specified name.static Node
getChildNode(Node parent, String childName)
Return the first child of the node with the specified name.static Node[]
getChildNodes(Node parent, String childName)
Return the children of the node with the specified name.static String
getFileContents(String fileName)
Extracts the contents of a file.static String
getTextContents(Node node)
Extracts the text content of a node.static void
main(String[] args)
Main method that reads an xsl and xml file and returns the transformation result.static String
node2Xml(Node node)
Converts a node to xml.static String
normalize(String xml)
Normalizes an xml string.static void
transform(String stylesheet, Reader in, Writer out)
Transforms xml from the input reader and writes the result to the writer.static String
transform(String stylesheet, String xmlData)
Transform xml with a stylesheet and return the result.
-
-
-
Method Detail
-
getChildContents
public static String getChildContents(Node node, String childName)
Return the text representation of the first child of the node with the specified name.- Parameters:
node
- The parent nodechildName
- The name of the child of which the text should be returned.- Returns:
- The first child node represetation with the specified name or
null
if such a child could not be found.
-
getChildNode
public static Node getChildNode(Node parent, String childName)
Return the first child of the node with the specified name.- Parameters:
node
- The parent nodechildName
- The name of the child that should be returned.- Returns:
- The first child node with the specified name or
null
if such a child could not be found.
-
getChildNodes
public static Node[] getChildNodes(Node parent, String childName)
Return the children of the node with the specified name.- Parameters:
node
- The parent nodechildName
- The name of the children that should be returned.- Returns:
- The child nodes with the specified name.
-
escapeToXML
public static String escapeToXML(String value)
Escapes special xml characters.- Parameters:
value
- The string that should be escaped.- Returns:
- The escapes xml representation of the
value
value argument.
-
getAttribute
public static String getAttribute(Node node, String attributeName)
Return the attribute with a specified name from a node.- Parameters:
node
- The parent node of which the attribute should be returned.attributeName
- The name of the attribute that should be matched.- Returns:
- The string contents of the matching attribute, or
null
if the attribute could not be found
-
getTextContents
public static String getTextContents(Node node)
Extracts the text content of a node.- Parameters:
node
- The node of which the text representation should be returned.- Returns:
- The text representation of the node or
null
if the argument is invalid.
-
createXmlDoc
public static Document createXmlDoc(String xml) throws ParserConfigurationException, SAXException, IOException
Creates a document for xml input.- Parameters:
xml
- The xml that should be parsed and converted to a document.- Returns:
- The document representation of the
xml
input. - Throws:
ParserConfigurationException
- if a DocumentBuilder cannot be created which satisfies the configuration requested.SAXException
- If the source could not be parsed.IOException
- If an IO error occurs.
-
normalize
public static String normalize(String xml) throws SAXException
Normalizes an xml string.- Parameters:
xml
- The xml that should be normalized- Returns:
- The normalized equivalent of the xml.
- Throws:
SAXException
- if the source could not be parsed.
-
node2Xml
public static String node2Xml(Node node) throws TransformerException
Converts a node to xml.- Parameters:
node
- The node for which the xml representation should be returned.- Returns:
- The xml source representation of the node.
- Throws:
TransformerException
- if the contents of the node could not be normalized.
-
doc2Xml
public static String doc2Xml(Document doc) throws TransformerException
Converts a document to xml.- Parameters:
node
- The document for which the xml representation should be returned.- Returns:
- The xml source representation of the document.
- Throws:
TransformerException
- if the contents of the document could not be normalized.
-
transform
public static String transform(String stylesheet, String xmlData) throws TransformerException
Transform xml with a stylesheet and return the result. The transformer is obtained from a pool so if you do multiple transformations with the same stylesheet, the transformer is reused from a pool.- Parameters:
stylesheet
- the xsl stylesheetxmlData
- the xml- Returns:
- result of transformation.
- Throws:
TransformerException
- if something goes wrong during the transformation
-
transform
public static void transform(String stylesheet, Reader in, Writer out) throws TransformerException
Transforms xml from the input reader and writes the result to the writer.- Parameters:
stylesheet
- the xsl stylesheetin
- the xml input readerout
- the writer for the transformation result- Throws:
TransformerException
- if something goes wrong during the transformation
-
main
public static void main(String[] args)
Main method that reads an xsl and xml file and returns the transformation result.- Parameters:
args
- The file arguments
-
getFileContents
public static String getFileContents(String fileName) throws IOException
Extracts the contents of a file.- Parameters:
fileName
- The absolute path to the file.- Returns:
- The string contents of the file.
- Throws:
IOException
- if the file could not be read.
-
-