com.jfw.util
Class XmlUtils

java.lang.Object
  extended by com.jfw.util.XmlUtils

public final class XmlUtils
extends java.lang.Object

Various utils for manage xml strings.


Constructor Summary
XmlUtils()
           
 
Method Summary
static java.lang.String changeEncode(java.lang.String xml, java.lang.String newEncode)
          Change the encode declared in xml tag with the encode declared in parameter newEncode.
static java.util.ArrayList getElements(org.w3c.dom.Document xmlDocument, java.lang.String parentTag)
          Get all values of tags inside an other tag.
static java.util.HashMap getElementsValues(org.w3c.dom.Document xmlDocument, java.util.ArrayList elements)
          Get all values of given tags.
static java.util.ArrayList getElementText(java.lang.String element, java.lang.String xml)
          Get all text delimited by a tag.
static java.util.ArrayList getElementValues(org.w3c.dom.Document xmlDocument, java.lang.String element)
          Get all values of a tag.
static java.lang.String getFirstValueOfTag(org.w3c.dom.Document xmlDocument, java.lang.String element)
          Get the first value of a tag.
static org.w3c.dom.Document getXMLDocument(java.lang.String xml, java.io.File xsdFile)
          Get a Document object.
static org.w3c.dom.Document getXMLDocument(java.lang.String path, java.lang.String fileName, java.io.File xsdFile)
          Get a Document object.
static boolean isUTF8(java.lang.String xml)
          Check if an xml string, declared in xml tag as UTF-8, use UTF-8 encode.
 
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

getXMLDocument

public static org.w3c.dom.Document getXMLDocument(java.lang.String xml,
                                                  java.io.File xsdFile)
                                           throws java.lang.Exception
Get a Document object.

Parameters:
xml - xml string.
xsdFile - an xsd file. If null then no xsd validation will done.
Returns:
a Document object.
Throws:
java.lang.Exception

getXMLDocument

public static org.w3c.dom.Document getXMLDocument(java.lang.String path,
                                                  java.lang.String fileName,
                                                  java.io.File xsdFile)
                                           throws java.lang.Exception
Get a Document object.

Parameters:
path - the path of the xml file.
fileName - the name of xml file.
xsdFile - an xsd file. If null then no xsd validation will done.
Returns:
a Document object.
Throws:
java.lang.Exception

getElementValues

public static java.util.ArrayList getElementValues(org.w3c.dom.Document xmlDocument,
                                                   java.lang.String element)
Get all values of a tag.

Parameters:
xmlDocument - a Document object.
element - the tag name.
Returns:
an ArrayList of String with the values of the tag.

getElementsValues

public static java.util.HashMap getElementsValues(org.w3c.dom.Document xmlDocument,
                                                  java.util.ArrayList elements)
Get all values of given tags.

Parameters:
xmlDocument - a Document object.
elements - an ArrayList of String with names of the tags.
Returns:
an ArrayList of String with the values of the all the tags.

getElementText

public static java.util.ArrayList getElementText(java.lang.String element,
                                                 java.lang.String xml)
Get all text delimited by a tag.

Parameters:
element - the name of the tag.
xml - the xml string.
Returns:
an ArrayList of String with the text find inside the tags decalred in element.

getElements

public static java.util.ArrayList getElements(org.w3c.dom.Document xmlDocument,
                                              java.lang.String parentTag)
Get all values of tags inside an other tag.

Parameters:
xmlDocument - a Document object.
parentTag - the name of the tag to elaborate.
Returns:
an ArrayList of HashMap. In every HashMap there are the tags found an there values.

getFirstValueOfTag

public static java.lang.String getFirstValueOfTag(org.w3c.dom.Document xmlDocument,
                                                  java.lang.String element)
Get the first value of a tag.

Parameters:
xmlDocument - a Document object.
element - the name of the tag.
Returns:
the first value of a tag.

isUTF8

public static boolean isUTF8(java.lang.String xml)
                      throws java.lang.Exception
Check if an xml string, declared in xml tag as UTF-8, use UTF-8 encode.

Parameters:
xml - the xml string to check.
Returns:
true if xml string encode is UTF-8 or declared encode is not UTF-8, false if other cases.
Throws:
java.lang.Exception

changeEncode

public static java.lang.String changeEncode(java.lang.String xml,
                                            java.lang.String newEncode)
Change the encode declared in xml tag with the encode declared in parameter newEncode.

Parameters:
xml - the xml to check.
newEncode - the encode to set in tag xml.
Returns:
the xml string with new encode.