Package org.apache.ofbiz.base.util
Class UtilXml
java.lang.Object
org.apache.ofbiz.base.util.UtilXml
Utilities methods to simplify dealing with JAXP and DOM XML parsing
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Local error handler for entity resolver to DocumentBuilder parser.static class
Local entity resolver to handle J2EE DTDs. -
Method Summary
Modifier and TypeMethodDescriptionstatic Element
addChildElement
(Element element, String childElementName, Document document) Creates a child element with the given name and appends it to the element child node list.static Element
addChildElementCDATAValue
(Element element, String childElementName, String childElementValue, Document document) Creates a child element with the given name and appends it to the element child node list.static Element
addChildElementNSElement
(Element element, String childElementName, Document document, String nameSpaceUrl) Creates a child element with the given namespace supportive name and appends it to the element child node list.static Element
addChildElementNSValue
(Element element, String childElementName, String childElementValue, Document document, String nameSpaceUrl) Creates a child element with the given namespace supportive name and appends it to the element child node list.static Element
addChildElementValue
(Element element, String childElementName, String childElementValue, Document document) Creates a child element with the given name and appends it to the element child node list.static boolean
checkBoolean
(String str) static boolean
checkBoolean
(String str, boolean defaultValue) static String
checkEmpty
(String string) static String
checkEmpty
(String string1, String string2) static String
checkEmpty
(String string1, String string2, String string3) static String
childElementAttribute
(Element element, String childElementName, String attributeName, String defaultValue) Return a named attribute of a named child node or a default if null.childElementList
(DocumentFragment fragment) Return a List of Element objects that are children of the given DocumentFragmentchildElementList
(Element element) Return a List of Element objects that are children of the given elementchildElementList
(Element element, String childElementName) Return a List of Element objects that have the given name and are immediate children of the given element; if name is null, all child elements will be included.childElementList
(Element element, String... childElementNames) Return a List of Element objects that have the given name and are immediate children of the given element; if name is null, all child elements will be included.childElementList
(Element element, Set<String> childElementNames) Return a List of Element objects that have the given name and are immediate children of the given element; if name is null, all child elements will be included.static String
childElementValue
(Element element, String childElementName) Return the text (node value) contained by the named child node.static String
childElementValue
(Element element, String childElementName, String defaultValue) Return the text (node value) contained by the named child node or a default value if null.childNodeList
(Node node) Return a List of Node objects that have the given name and are immediate children of the given element; if name is null, all child elements will be included.static String
convertDocumentToXmlString
(Document document) static Comment
createApacheLicenceComment
(Document document) Returns an ASF Licence Header as a Comment Elementstatic LSOutput
createLSOutput
(DOMImplementationLS impl, OutputStream os, String encoding) Returns aLSOutput
instance.static LSSerializer
createLSSerializer
(DOMImplementationLS impl, boolean includeXmlDeclaration, boolean enablePrettyPrint) Returns aLSSerializer
instance.static Transformer
createOutputTransformer
(String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount) Creates a JAXP TrAX Transformer suitable for pretty-printing an XML document.static String
elementAttribute
(Element element, String attrName, String defaultValue) static String
elementValue
(Element element) Return the text (node value) of the first node under this, works best if normalized.static Element
firstChildElement
(Element element) Return the first child Element returns the first element.static Element
firstChildElement
(Element element, String childElementName) Return the first child Element with the given name; if name is null returns the first element.static Element
firstChildElement
(Element element, String... childElementNames) Return the first child Element returns the first element.static Element
firstChildElement
(Element element, String childElementName, String attrName, String attrValue) Return the first child Element with the given name; if name is null returns the first element.static Element
firstChildElement
(Element element, Set<String> childElementNames) Return the first child Element returns the first element.static Object
fromXml
(InputStream input) Deserialize an object from anInputStream
.static Object
Deserialize an object from aReader
.static Object
Deserialize an object from aString
.static String
getAttributeValueIgnorePrefix
(Element element, String attributeName) get attribute value ignoring prefix in attribute namestatic DOMImplementationLS
Returns aDOMImplementationLS
instance.static String
getNodeNameIgnorePrefix
(Node node) get node name without any prefixstatic String
getTagNameIgnorePrefix
(Element element) get tag name without any prefixstatic Document
static Document
makeEmptyXmlDocument
(String rootElementName) static String
nodeNameToJavaName
(String nodeName, boolean capitalizeFirst) static String
Return the text (node value) of the first node under thisstatic Document
readXmlDocument
(InputStream is, boolean validate, String docDescription) static Document
readXmlDocument
(InputStream is, boolean validate, String docDescription, boolean withPosition) static Document
readXmlDocument
(InputStream is, String docDescription) static Document
readXmlDocument
(InputStream is, String docDescription, boolean withPosition) static Document
readXmlDocument
(String content) static Document
readXmlDocument
(String content, boolean validate) static Document
readXmlDocument
(String content, boolean validate, boolean withPosition) static Document
readXmlDocument
(URL url) static Document
readXmlDocument
(URL url, boolean validate) static Document
readXmlDocument
(URL url, boolean validate, boolean withPosition) static String
Serialize an object to an XMLString
.static void
toXml
(Object obj, OutputStream output) Serialize an object to anOutputStream
.static void
Serialize an object to aWriter
.static void
transformDomDocument
(Transformer transformer, Node node, OutputStream os) Serializes a DOMNode
to anOutputStream
using JAXP TrAX.static void
writeXmlDocument
(OutputStream os, Node node) static void
writeXmlDocument
(OutputStream os, Node node, String encoding, boolean includeXmlDeclaration, boolean enablePrettyPrint) Serializes a DOM Node to anOutputStream
using DOM 3.static void
writeXmlDocument
(String filename, Node node) static String
writeXmlDocument
(Node node) static void
writeXmlDocument
(Node node, OutputStream os, String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount) Serializes a DOMNode
to anOutputStream
using JAXP TrAX.
-
Method Details
-
getDomLsImplementation
public static DOMImplementationLS getDomLsImplementation() throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessExceptionReturns aDOMImplementationLS
instance.- Returns:
- A
DOMImplementationLS
instance - Throws:
ClassCastException
ClassNotFoundException
InstantiationException
IllegalAccessException
- See Also:
-
createLSOutput
Returns aLSOutput
instance.- Parameters:
impl
- ADOMImplementationLS
instanceos
- OptionalOutputStream
instanceencoding
- Optional character encoding, default is UTF-8- Returns:
- A
LSOutput
instance - See Also:
-
createLSSerializer
public static LSSerializer createLSSerializer(DOMImplementationLS impl, boolean includeXmlDeclaration, boolean enablePrettyPrint) Returns aLSSerializer
instance.- Parameters:
impl
- ADOMImplementationLS
instanceincludeXmlDeclaration
- If set totrue
, the xml declaration will be included in the outputenablePrettyPrint
- If set totrue
, the output will be formatted in human-readable form. If set tofalse
, the entire document will consist of a single line.- Returns:
- A
LSSerializer
instance - See Also:
-
writeXmlDocument
public static void writeXmlDocument(OutputStream os, Node node, String encoding, boolean includeXmlDeclaration, boolean enablePrettyPrint) throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException Serializes a DOM Node to anOutputStream
using DOM 3.- Parameters:
os
- TheOutputStream
instance to write tonode
- The DOMNode
object to be serializedencoding
- Optional character encodingincludeXmlDeclaration
- If set totrue
, the xml declaration will be included in the outputenablePrettyPrint
- If set totrue
, the output will be formatted in human-readable form. If set tofalse
, the entire document will consist of a single line.- Throws:
ClassCastException
ClassNotFoundException
InstantiationException
IllegalAccessException
- See Also:
-
createOutputTransformer
public static Transformer createOutputTransformer(String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount) throws TransformerConfigurationException Creates a JAXP TrAX Transformer suitable for pretty-printing an XML document. This method is provided as an alternative to the deprecatedorg.apache.xml.serialize.OutputFormat
class.- Parameters:
encoding
- Optional encoding, defaults to UTF-8omitXmlDeclaration
- Iftrue
the xml declaration will be omitted from the outputindent
- Iftrue
, the output will be indentedindentAmount
- Ifindent
istrue
, the number of spaces to indent. Default is 4.- Returns:
- A
Transformer
instance - Throws:
TransformerConfigurationException
- See Also:
-
transformDomDocument
public static void transformDomDocument(Transformer transformer, Node node, OutputStream os) throws TransformerException Serializes a DOMNode
to anOutputStream
using JAXP TrAX.- Parameters:
transformer
- ATransformer
instancenode
- TheNode
to serializeos
- TheOutputStream
to serialize to- Throws:
TransformerException
- See Also:
-
writeXmlDocument
public static void writeXmlDocument(Node node, OutputStream os, String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount) throws TransformerException Serializes a DOMNode
to anOutputStream
using JAXP TrAX.- Parameters:
node
- TheNode
to serializeos
- TheOutputStream
to serialize toencoding
- Optional encoding, defaults to UTF-8omitXmlDeclaration
- Iftrue
the xml declaration will be omitted from the outputindent
- Iftrue
, the output will be indentedindentAmount
- Ifindent
istrue
, the number of spaces to indent. Default is 4.- Throws:
TransformerException
- See Also:
-
fromXml
Deserialize an object from anInputStream
.- Parameters:
input
- TheInputStream
- Returns:
- The deserialized
Object
-
fromXml
Deserialize an object from aReader
.- Parameters:
reader
- TheReader
- Returns:
- The deserialized
Object
-
fromXml
Deserialize an object from aString
.- Parameters:
str
- TheString
- Returns:
- The deserialized
Object
-
toXml
Serialize an object to an XMLString
.- Parameters:
obj
- The object to serialize- Returns:
- An XML
String
-
toXml
Serialize an object to anOutputStream
.- Parameters:
obj
- The object to serializeoutput
- TheOutputStream
-
toXml
Serialize an object to aWriter
.- Parameters:
obj
- The object to serializewriter
- TheWriter
-
writeXmlDocument
- Throws:
IOException
-
writeXmlDocument
public static void writeXmlDocument(String filename, Node node) throws FileNotFoundException, IOException - Throws:
FileNotFoundException
IOException
-
writeXmlDocument
- Throws:
IOException
-
readXmlDocument
public static Document readXmlDocument(String content) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(String content, boolean validate) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(String content, boolean validate, boolean withPosition) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(URL url) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(URL url, boolean validate) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(URL url, boolean validate, boolean withPosition) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(InputStream is, String docDescription) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(InputStream is, String docDescription, boolean withPosition) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(InputStream is, boolean validate, String docDescription) throws SAXException, ParserConfigurationException, IOException -
readXmlDocument
public static Document readXmlDocument(InputStream is, boolean validate, String docDescription, boolean withPosition) throws SAXException, ParserConfigurationException, IOException -
makeEmptyXmlDocument
-
makeEmptyXmlDocument
-
addChildElement
Creates a child element with the given name and appends it to the element child node list. -
addChildElementValue
public static Element addChildElementValue(Element element, String childElementName, String childElementValue, Document document) Creates a child element with the given name and appends it to the element child node list. Also creates a Text node with the given value and appends it to the new elements child node list. -
addChildElementNSElement
public static Element addChildElementNSElement(Element element, String childElementName, Document document, String nameSpaceUrl) Creates a child element with the given namespace supportive name and appends it to the element child node list. -
addChildElementNSValue
public static Element addChildElementNSValue(Element element, String childElementName, String childElementValue, Document document, String nameSpaceUrl) Creates a child element with the given namespace supportive name and appends it to the element child node list. Also creates a Text node with the given value and appends it to the new elements child node list. -
addChildElementCDATAValue
public static Element addChildElementCDATAValue(Element element, String childElementName, String childElementValue, Document document) Creates a child element with the given name and appends it to the element child node list. Also creates a CDATASection node with the given value and appends it to the new elements child node list. -
childElementList
Return a List of Element objects that are children of the given element -
childElementList
Return a List of Element objects that have the given name and are immediate children of the given element; if name is null, all child elements will be included. -
childElementList
public static List<? extends Element> childElementList(Element element, Set<String> childElementNames) Return a List of Element objects that have the given name and are immediate children of the given element; if name is null, all child elements will be included. -
childElementList
public static List<? extends Element> childElementList(Element element, String... childElementNames) Return a List of Element objects that have the given name and are immediate children of the given element; if name is null, all child elements will be included. -
childElementList
Return a List of Element objects that are children of the given DocumentFragment -
childNodeList
Return a List of Node objects that have the given name and are immediate children of the given element; if name is null, all child elements will be included. -
firstChildElement
Return the first child Element returns the first element. -
firstChildElement
Return the first child Element returns the first element. -
firstChildElement
Return the first child Element returns the first element. -
firstChildElement
Return the first child Element with the given name; if name is null returns the first element. -
firstChildElement
public static Element firstChildElement(Element element, String childElementName, String attrName, String attrValue) Return the first child Element with the given name; if name is null returns the first element. -
childElementValue
Return the text (node value) contained by the named child node. -
childElementValue
public static String childElementValue(Element element, String childElementName, String defaultValue) Return the text (node value) contained by the named child node or a default value if null. -
childElementAttribute
public static String childElementAttribute(Element element, String childElementName, String attributeName, String defaultValue) Return a named attribute of a named child node or a default if null. -
elementValue
Return the text (node value) of the first node under this, works best if normalized. -
nodeValue
Return the text (node value) of the first node under this -
elementAttribute
-
checkEmpty
-
checkEmpty
-
checkEmpty
-
checkBoolean
-
checkBoolean
-
nodeNameToJavaName
-
getNodeNameIgnorePrefix
get node name without any prefix- Parameters:
node
-- Returns:
- nodeName
-
getTagNameIgnorePrefix
get tag name without any prefix- Parameters:
element
-- Returns:
- tagName
-
getAttributeValueIgnorePrefix
get attribute value ignoring prefix in attribute name- Parameters:
element
-- Returns:
- The value of the node, depending on its type; see the table Node class
-
convertDocumentToXmlString
-
createApacheLicenceComment
Returns an ASF Licence Header as a Comment Element- Parameters:
document
-- Returns:
-