Class XMLUtils
java.lang.Object
org.bgerp.util.xml.XMLUtils
- Direct Known Subclasses:
XMLUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcreateTextNode(Node node, String text) Creates a text child node.Makes an Iterable of Element from NodeList, for convenient iterationstatic ElementgetElement(Document doc, String elementName) Searches for an element in the document by name.static StringgetElementText(Node node) Extracts content of a XML element as a string with all sub-tagsstatic StringgetElementText(Node node, Set<String> skipNodeNames) Extracts content of a XML element as a string with all sub-tagsstatic NodeGets a Node by tag name from Document.static final DocumentCreates and returns a new XML document objectstatic final ElementnewElement(Document parent, String name) Creates an element on the parent document.static final ElementnewElement(Element parent, String name) Creates a node object with the given name in the parent nodestatic DocumentparseDocument(InputStream stream) static DocumentparseDocument(InputSource source) static ElementselectElement(Node node, String expression) Returns element by XPath expressionselectElements(Node node, String expression) static NodeselectNode(Node node, String expression) Returns Node by XPath expression Example: /data/table - select the table element at the root Example: //table - select the table element anywherestatic NodeListselectNodeList(Node node, String expression) Returns NodeList by XPath expressionstatic StringselectText(Node node, String expression) Selection of a string value by xpath.static StringselectText(Node node, String expression, String defaultValue) Selection of a string value by xpath.static voidserialize(Node xml, OutputStream result, String encoding, boolean pretty)
-
Constructor Details
-
XMLUtils
public XMLUtils()
-
-
Method Details
-
getElementText
-
getElementText
-
newDocument
Creates and returns a new XML document object- Returns:
- the created document
-
newElement
-
newElement
-
createTextNode
-
parseDocument
-
parseDocument
-
getElement
-
getNode
-
selectElement
-
selectNode
-
selectNodeList
-
selectElements
-
elements
-
selectText
-
selectText
Selection of a string value by xpath. That is, practically the same as selectNode, only it returns the node value instead, ornullif nothing is found or some error occurs (should not return text etc.) Can return both text node values and attribute values, universal.
In general, not intended for selections like selectText(node, "@selected", null), because it's not a problem that getAttribute returns notnullbut an empty string. Moreover, there is a method that both returnsnulland works faster.
Query examples:
1) query for an attribute's text value<data><payment cardnumber="111">... /data/payment/@cardnumber
2) query for a node's text value...<operation><pursesrc>text</pursesrc>... /operation/pursesrc/text()
3) not from the root, but relative - not starting with a slash
4) search anywhere - double slash
5) etc., you get the idea- Parameters:
node- the root nodeexpression- xpathdefaultValue- the default value- Returns:
- the string
- See Also:
-
serialize
public static void serialize(Node xml, OutputStream result, String encoding, boolean pretty) throws Exception - Throws:
Exception
-