This class with handle some simple XML operations.
Method from org.apache.jempbox.impl.XMLUtil Detail: |
public static byte[] asByteArray(Document doc,
String encoding) throws TransformerException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return writer.getBuffer().toString().getBytes();
}
Convert the document to an array of bytes. |
public static Element getElement(Element parent,
String elementName) {
Element retval = null;
NodeList children = parent.getElementsByTagName( elementName );
if( children.getLength() > 0 )
{
retval = (Element)children.item( 0 );
}
return retval;
}
Get the first instance of an element by name. |
public static Integer getIntValue(Element parent,
String nodeName) {
String intVal = XMLUtil.getStringValue( XMLUtil.getElement( parent, nodeName ) );
Integer retval = null;
if( intVal != null )
{
retval = new Integer( intVal );
}
return retval;
}
Get the integer value of a subnode. |
public static String getStringValue(Element node) {
String retval = "";
NodeList children = node.getChildNodes();
for( int i=0; i< children.getLength(); i++ )
{
Node next = children.item( i );
if( next instanceof Text )
{
retval = next.getNodeValue();
}
}
return retval;
}
This will get the text value of an element. |
public static String getStringValue(Element parent,
String nodeName) {
return XMLUtil.getStringValue( XMLUtil.getElement( parent, nodeName ) );
}
Get the value of a subnode. |
public static Document newDocument() throws IOException {
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
return builder.newDocument();
}
catch( Exception e )
{
IOException thrown = new IOException( e.getMessage() );
throw thrown;
}
}
Create a new blank XML document. |
public static Document parse(InputStream is) throws IOException {
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
return builder.parse( is );
}
catch( Exception e )
{
IOException thrown = new IOException( e.getMessage() );
throw thrown;
}
}
This will parse an XML stream and create a DOM document. |
public static Document parse(InputSource is) throws IOException {
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
return builder.parse( is );
}
catch( Exception e )
{
IOException thrown = new IOException( e.getMessage() );
throw thrown;
}
}
This will parse an InputSource and create a DOM document. |
public static Document parse(String fileName) throws IOException {
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
return builder.parse( fileName );
}
catch( Exception e )
{
IOException thrown = new IOException( e.getMessage() );
throw thrown;
}
}
This will parse an XML stream and create a DOM document. |
public static void save(Document doc,
String file,
String encoding) throws TransformerException {
try
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
// initialize StreamResult with File object to save to file
Result result = new StreamResult(new File(file));
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
}
finally
{
}
}
Save the XML document to a file. |
public static void save(Node doc,
OutputStream outStream,
String encoding) throws TransformerException {
try
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
// initialize StreamResult with File object to save to file
Result result = new StreamResult(outStream);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
}
finally
{
}
}
Save the XML document to an output stream. |
public static void setElementableValue(Element parent,
String name,
Elementable node) {
NodeList nodes = parent.getElementsByTagName( name );
if( node == null )
{
for( int i=0; i< nodes.getLength(); i++ )
{
parent.removeChild( nodes.item( i ) );
}
}
else
{
if( nodes.getLength() == 0 )
{
if( parent.hasChildNodes() )
{
Node firstChild = parent.getChildNodes().item( 0 );
parent.insertBefore( node.getElement(), firstChild );
}
else
{
parent.appendChild( node.getElement() );
}
}
else
{
Node oldNode = nodes.item( 0 );
parent.replaceChild( node.getElement(), oldNode );
}
}
}
Set an XML element document. |
public static void setIntValue(Element parent,
String nodeName,
Integer intValue) {
Element currentValue = getElement( parent, nodeName );
if( intValue == null )
{
if( currentValue != null )
{
parent.removeChild( currentValue );
}
else
{
//it doesn't exist so we don't need to remove it.
}
}
else
{
if( currentValue == null )
{
currentValue = parent.getOwnerDocument().createElement( nodeName );
parent.appendChild( currentValue );
}
XMLUtil.setStringValue( currentValue, intValue.toString() );
}
}
Set the integer value of an element. |
public static void setStringValue(Element node,
String value) {
NodeList children = node.getChildNodes();
for( int i=0; i< children.getLength(); i++ )
{
Node next = children.item( i );
if( next instanceof Text )
{
node.removeChild( next );
}
}
node.appendChild( node.getOwnerDocument().createTextNode( value ) );
}
This will set the text value of an element. |
public static void setStringValue(Element parent,
String nodeName,
String nodeValue) {
Element currentValue = getElement( parent, nodeName );
if( nodeValue == null )
{
if( currentValue != null )
{
parent.removeChild( currentValue );
}
else
{
//it doesn't exist so we don't need to remove it.
}
}
else
{
if( currentValue == null )
{
currentValue = parent.getOwnerDocument().createElement( nodeName );
parent.appendChild( currentValue );
}
XMLUtil.setStringValue( currentValue, nodeValue );
}
}
Set the value of an element. |