Package org.cyclos.server.utils
Class XmlHelper
- java.lang.Object
-
- org.cyclos.server.utils.XmlHelper
-
public class XmlHelper extends Object
Contains helper methods for handling XML
-
-
Constructor Summary
Constructors Constructor Description XmlHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ElementcreateChild(Element element, String tag, String contents)Creates a sub-element on the given element.static Stringexport(Node node)Exports the given node and his sub-elements as Stringstatic voidexport(Node node, Writer out)Exports the given node and his sub-elements into the given output streamstatic booleanfillAttributes(Element element, Object bean, String... properties)Fills the given element's attributes by getting the value for each specified bean property, returning whether there was at least one non-empty attribute.static booleanfillPropertiesWithAttributes(Object bean, Element element, String... properties)Fills the given beans's specified properties by getting the value for each property as attributes of the given root element.static booleanfillPropertiesWithSubElements(Object bean, Element element, String... properties)Fills the given beans's specified properties by getting the value for each property as sub element's of the given root element.static booleanfillSubElements(Element element, Object bean, String... properties)Adds several sub-elements whose texts are retrieved from each bean property element.static ElementgetFirst(Element element, String subElementTag)Returns the first child element with the given tag namestatic StringgetText(Element element)Returns the text inside an elementstatic StringgetText(Element element, String subElementTag)Returns the text inside the first element's children with the given tag namestatic NodeListElementIteratoriterate(Element root)Iterates over the child elements on the given root elementstatic NodeListElementIteratoriterate(Element root, String tagName)Iterates over the child elements with the given tag name, on the given root elementstatic NodeListIteratoriterateChildren(Element root)Iterates over the child nodes with the given tag name, on the given root elementstatic DocumentnewDocument()Builds a new documentstatic Documentparse(InputStream in)Parses a document input streamstatic Documentparse(InputStream in, EntityResolver entityResolver)Parses a document input streamstatic Documentparse(String xml)Parses a document stringstatic Documentparse(String xml, EntityResolver entityResolver)Parses a document stringstatic voidparse(DefaultHandler saxHandler, InputStream in)Parses a xml using SAX, with the given content handlerstatic voidparse(DefaultHandler saxHandler, String xml)Parses a xml using SAX, with the given content handlerstatic voidparse(DefaultHandler saxHandler, InputSource source)Parses a xml using SAX, with the given content handlerstatic StringpropertyToTag(String property)Transforms the given property name into something suitable for tag or attribute name.
-
-
-
Method Detail
-
createChild
public static Element createChild(Element element, String tag, String contents)
Creates a sub-element on the given element. The new sub-element will contain the given string as text, and will be returned. The contents will be trimmed.
-
export
public static String export(Node node)
Exports the given node and his sub-elements as String
-
export
public static void export(Node node, Writer out)
Exports the given node and his sub-elements into the given output stream
-
fillAttributes
public static boolean fillAttributes(Element element, Object bean, String... properties)
Fills the given element's attributes by getting the value for each specified bean property, returning whether there was at least one non-empty attribute. Attribute names are transformed from property names usingpropertyToTag(String).
-
fillPropertiesWithAttributes
public static boolean fillPropertiesWithAttributes(Object bean, Element element, String... properties)
Fills the given beans's specified properties by getting the value for each property as attributes of the given root element.
-
fillPropertiesWithSubElements
public static boolean fillPropertiesWithSubElements(Object bean, Element element, String... properties)
Fills the given beans's specified properties by getting the value for each property as sub element's of the given root element. Tag names are transformed from property names usingpropertyToTag(String).- See Also:
getText(Element, String)
-
fillSubElements
public static boolean fillSubElements(Element element, Object bean, String... properties)
Adds several sub-elements whose texts are retrieved from each bean property element. Tag names are transformed from property names usingpropertyToTag(String).
-
getFirst
public static Element getFirst(Element element, String subElementTag)
Returns the first child element with the given tag name
-
getText
public static String getText(Element element, String subElementTag)
Returns the text inside the first element's children with the given tag name
-
iterate
public static NodeListElementIterator iterate(Element root)
Iterates over the child elements on the given root element
-
iterate
public static NodeListElementIterator iterate(Element root, String tagName)
Iterates over the child elements with the given tag name, on the given root element
-
iterateChildren
public static NodeListIterator iterateChildren(Element root)
Iterates over the child nodes with the given tag name, on the given root element
-
newDocument
public static Document newDocument()
Builds a new document
-
parse
public static void parse(DefaultHandler saxHandler, InputSource source) throws SAXException, IOException
Parses a xml using SAX, with the given content handler- Throws:
SAXExceptionIOException
-
parse
public static void parse(DefaultHandler saxHandler, InputStream in) throws SAXException, IOException
Parses a xml using SAX, with the given content handler- Throws:
SAXExceptionIOException
-
parse
public static void parse(DefaultHandler saxHandler, String xml) throws SAXException, IOException
Parses a xml using SAX, with the given content handler- Throws:
SAXExceptionIOException
-
parse
public static Document parse(InputStream in) throws SAXException, IOException
Parses a document input stream- Throws:
SAXExceptionIOException
-
parse
public static Document parse(InputStream in, EntityResolver entityResolver) throws SAXException, IOException
Parses a document input stream- Throws:
SAXExceptionIOException
-
parse
public static Document parse(String xml) throws SAXException, IOException
Parses a document string- Throws:
SAXExceptionIOException
-
parse
public static Document parse(String xml, EntityResolver entityResolver) throws SAXException, IOException
Parses a document string- Throws:
SAXExceptionIOException
-
-