gr.uoa.di.madgik.commons.utils
Class XMLUtils

java.lang.Object
  extended by gr.uoa.di.madgik.commons.utils.XMLUtils

public class XMLUtils
extends Object

Utility class to parse and traverse an XML DOM tree

Author:
gpapanikos

Constructor Summary
XMLUtils()
           
 
Method Summary
static Boolean AttributeExists(Element node, String attributeName)
          Checks if the provided attribute exists in the supplied node
static Document Deserialize(File XML)
           
static Document Deserialize(String XML)
          Parses the provided XML string
static String DoReplaceSpecialCharachters(String XML)
          Replaces special characters with xml valid escape sequences.
static String GetAttribute(Element node, String attributeName)
          Retrieves an attribute's value
static String[] GetAttributes(Element node, boolean CollapseNamespase)
           
static String GetChildCDataText(Node Parent)
           
static List<Element> GetChildElementsWithName(Node Parent, String name)
          Retrieves all child elements of the provided node that have the provided name
static List<Element> GetChildElementsWithNameAndNamespace(Node Parent, String name, String ns)
           
static Element GetChildElementWithName(Node Parent, String name)
          Retrieves a single child element of the provided node that has the provided name.
static Element GetChildElementWithNameAndNamespace(Node Parent, String name, String ns)
           
static String GetChildText(Node Parent)
          Retrieves the text payload of the first available Text node that is a member of the provided node children
static String GetChildTextOrFirstNonEmptyChildElementAsText(Node Parent)
           
static Object Query(Document doc, String query, QName type)
           
static String Serialize(Node node)
           
static String Serialize(Node node, boolean omitDeclaration)
           
static void Serialize(String FileName, String XML)
           
static String SerializeChild(Element parent)
           
static String Transform(String Source, String xslt)
           
static String UndoReplaceSpecialCharachters(String XML)
          Replaces valid xml escape sequences to their original form
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

Serialize

public static void Serialize(String FileName,
                             String XML)
                      throws Exception
Throws:
Exception

Serialize

public static String Serialize(Node node,
                               boolean omitDeclaration)
                        throws Exception
Throws:
Exception

Serialize

public static String Serialize(Node node)
                        throws Exception
Throws:
Exception

Deserialize

public static Document Deserialize(String XML)
                            throws Exception
Parses the provided XML string

Parameters:
XML - the xml to parse
Returns:
the Document constructed
Throws:
Exception - the deserialization could not be performed

Deserialize

public static Document Deserialize(File XML)
                            throws Exception
Throws:
Exception

AttributeExists

public static Boolean AttributeExists(Element node,
                                      String attributeName)
                               throws Exception
Checks if the provided attribute exists in the supplied node

Parameters:
node - the Node that should hold the attribute
attributeName - the name of the attribute to check for
Returns:
whether or not the attribute exists
Throws:
Exception - The operation could not be performed

GetAttribute

public static String GetAttribute(Element node,
                                  String attributeName)
                           throws Exception
Retrieves an attribute's value

Parameters:
node - The node the attribute should exist in
attributeName - the name of the attribute
Returns:
the value of the attribute
Throws:
Exception - The attribute does not exist

GetAttributes

public static String[] GetAttributes(Element node,
                                     boolean CollapseNamespase)
                              throws Exception
Throws:
Exception

SerializeChild

public static String SerializeChild(Element parent)
                             throws Exception
Throws:
Exception

GetChildElementWithName

public static Element GetChildElementWithName(Node Parent,
                                              String name)
                                       throws Exception
Retrieves a single child element of the provided node that has the provided name. If more than one elements have the same name, only the first one is returned

Parameters:
Parent - the node to check its children
name - the name of the child node to retrieve
Returns:
the child element or null if it doesn't exist
Throws:
Exception - the Operation could not be performed

GetChildElementWithNameAndNamespace

public static Element GetChildElementWithNameAndNamespace(Node Parent,
                                                          String name,
                                                          String ns)
                                                   throws Exception
Throws:
Exception

GetChildElementsWithName

public static List<Element> GetChildElementsWithName(Node Parent,
                                                     String name)
                                              throws Exception
Retrieves all child elements of the provided node that have the provided name

Parameters:
Parent - the node to check its children
name - the name of the child node to retrieve
Returns:
the child element or null if it doesn't exist
Throws:
Exception - the Operation could not be performed

GetChildElementsWithNameAndNamespace

public static List<Element> GetChildElementsWithNameAndNamespace(Node Parent,
                                                                 String name,
                                                                 String ns)
                                                          throws Exception
Throws:
Exception

GetChildTextOrFirstNonEmptyChildElementAsText

public static String GetChildTextOrFirstNonEmptyChildElementAsText(Node Parent)
                                                            throws Exception
Throws:
Exception

GetChildCDataText

public static String GetChildCDataText(Node Parent)
                                throws Exception
Throws:
Exception

GetChildText

public static String GetChildText(Node Parent)
                           throws Exception
Retrieves the text payload of the first available Text node that is a member of the provided node children

Parameters:
Parent - The node whose child text node's payload should be retrieved
Returns:
The text or null if no text node could be found
Throws:
Exception - the Operation could not be performed

Query

public static Object Query(Document doc,
                           String query,
                           QName type)
                    throws Exception
Throws:
Exception

Transform

public static String Transform(String Source,
                               String xslt)
                        throws Exception
Throws:
Exception

DoReplaceSpecialCharachters

public static String DoReplaceSpecialCharachters(String XML)
Replaces special characters with xml valid escape sequences.

Parameters:
XML - The xml to escape
Returns:
the escaped xml

UndoReplaceSpecialCharachters

public static String UndoReplaceSpecialCharachters(String XML)
Replaces valid xml escape sequences to their original form

Parameters:
XML - the escaped xml
Returns:
the original xml


Copyright © 2013. All Rights Reserved.