Class XmlDomUtils

java.lang.Object
gov.nasa.pds.registry.common.util.xml.XmlDomUtils

public class XmlDomUtils extends Object
Utility class to work with XML DOM model. NOTE: Don't use XML DOM API for big files. DOM API loads the whole XML file into memory.
Author:
karpenko
  • Constructor Details

    • XmlDomUtils

      public XmlDomUtils()
  • Method Details

    • readXml

      public static Document readXml(DocumentBuilderFactory dbf, File file) throws Exception
      Parse XML file and create a DOM model. This method reads the whole XML document into memory.
      Parameters:
      dbf - Document builder factory
      file - XML file to parse
      Returns:
      XML DOM model
      Throws:
      Exception - Generic exception
    • readXml

      public static Document readXml(File file) throws Exception
      Parse XML file and create a DOM model. This method reads the whole XML document into memory.
      Parameters:
      file - XML file to parse
      Returns:
      XML DOM model
      Throws:
      Exception - Generic exception
    • readXml

      public static Document readXml(File xmlFile, File xsdFile, ErrorHandler eh) throws Exception
      Parse XML file and create a DOM model. This method reads the whole XML document into memory.
      Parameters:
      xmlFile - XML file to parse
      xsdFile - XSD schema file for XML validation
      eh - custom error handler
      Returns:
      XML DOM model
      Throws:
      Exception - Generic exception
    • getAttribute

      public static String getAttribute(Node node, String attributeName)
      Get attribute of a node by name.
      Parameters:
      node - DOM node
      attributeName - attribute name
      Returns:
      attribute value
    • getAttributes

      public static NamedNodeMap getAttributes(Node node)
      Get all attributes of a node.
      Parameters:
      node - DOM node
      Returns:
      attribute map
    • countChildNodes

      public static int countChildNodes(Node node, String name)
      Count child nodes.
      Parameters:
      node - Parent node object
      name - child node name
      Returns:
      node count
    • getFirstChild

      public static Node getFirstChild(Node node, String name)
      Get first child node.
      Parameters:
      node - parent node
      name - child node name
      Returns:
      a child node
    • getChildNodes

      public static List<Node> getChildNodes(Node node, String name)
      Get all child nodes.
      Parameters:
      node - parent node.
      name - child node name.
      Returns:
      a list of nodes