Class XMLUtils

java.lang.Object
org.bgerp.util.xml.XMLUtils
Direct Known Subclasses:
XMLUtils

public class XMLUtils extends Object
  • Constructor Details

    • XMLUtils

      public XMLUtils()
  • Method Details

    • getElementText

      public static String getElementText(Node node)
      Extracts content of a XML element as a string with all sub-tags
      Parameters:
      node - the element node
      Returns:
      the nodes' content
    • getElementText

      public static String getElementText(Node node, Set<String> skipNodeNames)
      Extracts content of a XML element as a string with all sub-tags
      Parameters:
      node - the element node
      skipNodeNames - if not null - node names to be skipped
      Returns:
      the nodes' content
    • newDocument

      public static final Document newDocument()
      Creates and returns a new XML document object
      Returns:
      the created document
    • newElement

      public static final Element newElement(Element parent, String name)
      Creates a node object with the given name in the parent node
      Parameters:
      parent - the parent node
      name - the new node's name
      Returns:
      the created element
    • newElement

      public static final Element newElement(Document parent, String name)
      Creates an element on the parent document. The Element overload never works (OwnerDocument is always null), and getDocumentElement doesn't help either.
      Parameters:
      parent -
      name -
      Returns:
      the created element
    • createTextNode

      public static void createTextNode(Node node, String text)
      Creates a text child node. In other words, sets text inside the specified node. If the node is /data, the result will be <data>text</data>
      Parameters:
      node - the node
      text - the text
    • parseDocument

      public static Document parseDocument(InputStream stream)
    • parseDocument

      public static Document parseDocument(InputSource source)
    • getElement

      public static Element getElement(Document doc, String elementName)
      Searches for an element in the document by name. If not found - returns a created one.
      Parameters:
      doc -
      elementName -
      Returns:
      the found or created element
    • getNode

      public static Node getNode(Document doc, String nodeName)
      Gets a Node by tag name from Document. If absent - creates a new Node and returns it.
      Parameters:
      doc - the document
      nodeName - the node name
      Returns:
      the node
      See Also:
    • selectElement

      public static Element selectElement(Node node, String expression)
      Returns element by XPath expression
      Parameters:
      node -
      expression - XPath expression
      Returns:
      the element, if found - otherwise null
      See Also:
    • selectNode

      public static Node selectNode(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 anywhere
      Parameters:
      node -
      expression - XPath expression
      Returns:
      Node, if found - otherwise null
    • selectNodeList

      public static NodeList selectNodeList(Node node, String expression)
      Returns NodeList by XPath expression
      Parameters:
      node -
      expression - XPath expression
      Returns:
      NodeList, if found - otherwise null
    • selectElements

      public static Iterable<Element> selectElements(Node node, String expression)
    • elements

      public static Iterable<Element> elements(NodeList nodeList)
      Makes an Iterable of Element from NodeList, for convenient iteration
      Parameters:
      nodeList - the source NodeList
      Returns:
      Iterable<Element>
    • selectText

      public static String selectText(Node node, String expression)
      Selection of a string value by xpath. Default value - null.
      See Also:
    • selectText

      public static String selectText(Node node, String expression, String defaultValue)
      Selection of a string value by xpath. That is, practically the same as selectNode, only it returns the node value instead, or null if 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 not null but an empty string. Moreover, there is a method that both returns null and 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 node
      expression - xpath
      defaultValue - 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