XML and XPath utilities : XPath « XML « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » XML » XPathScreenshots 
XML and XPath utilities
  

// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.


import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Some XML and XPath utilities.
 */
public class XmlUtil {


  // ---------------------------------------------------------------- attributes

  /**
   * Returns a map of all node's attributes. All non-attribute nodes are ignored.
   */
  public static Map<String, String> getAllAttributes(Node node) {
    HashMap<String, String> attrs = new HashMap<String, String>();
    NamedNodeMap nmm = node.getAttributes();
    for (int j = 0; j < nmm.getLength(); j++) {
      Node attribute = nmm.item(j);
      if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
        continue;
      }
      attrs.put(attribute.getNodeName(), attribute.getNodeValue());
    }
    return attrs;
  }

    /**
   * Returns attribute value of a node or <code>null</code> if attribute name not found.
   * Specified attribute is searched on every call.
   * Consider {@link #getAllAttributes(org.w3c.dom.Node)} for better performances.
   */
  public static String getAttributeValue(Node node, String attrName) {
    NamedNodeMap nmm = node.getAttributes();
    for (int j = 0; j < nmm.getLength(); j++) {
      Node attribute = nmm.item(j);
      if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
        continue;
      }
      String nodeName = attribute.getNodeName();
      if (nodeName.equals(attrName)) {
        return attribute.getNodeValue();
      }
    }
    return null;
  }

  /**
   * Get element's attribute value or <code>null</code> if attribute not found or empty.
   */
  public static String getAttributeValue(Element element, String name) {
    String value = element.getAttribute(name);
    if (value.length() == 0) {
      value = null;
    }
    return value;
  }


  // ---------------------------------------------------------------- nodelist

  /**
   * Filters node list by keeping nodes of specified type.
   */
  public static List filterNodeList(NodeList nodeList, short keepNodeType) {
    return filterNodeList(nodeList, keepNodeType, null);
  }

  /**
   * Filters node list by keeping nodes of specified type and node name.
   */
  public static List<Node> filterNodeList(NodeList nodeList, short keepNodeType, String nodeName) {
    List<Node> nodes = new ArrayList<Node>();
    for (int k = 0; k < nodeList.getLength(); k++) {
      Node node = nodeList.item(k);
      if (node.getNodeType() != keepNodeType) {
        continue;
      }
      if (nodeName != null && (node.getNodeName().equals(nodeName== false)) {
        continue;
      }
      nodes.add(node);
    }
    return nodes;
  }

  /**
   * Filter node list for all Element nodes.
   */
  public static List filterNodeListElements(NodeList nodeList) {
    return filterNodeListElements(nodeList, null);
  }

  /**
   * Filter node list for Element nodes of specified name.
   */
  public static List<Node> filterNodeListElements(NodeList nodeList, String nodeName) {
    List<Node> nodes = new ArrayList<Node>();
    for (int k = 0; k < nodeList.getLength(); k++) {
      Node node = nodeList.item(k);
      if (node.getNodeType() != Node.ELEMENT_NODE) {
        continue;
      }
      if (nodeName != null && (node.getNodeName().equals(nodeName== false)) {
        continue;
      }
      nodes.add(node);
    }
    return nodes;
  }


  /**
   * Returns a list of all child Elements,
   */
  public static List getChildElements(Node node) {
    return getChildElements(node, null);
  }


  /**
   * Returns a list of child Elements of specified name.
   */
  public static List getChildElements(Node node, String nodeName) {
    NodeList childs = node.getChildNodes();
    return filterNodeListElements(childs, nodeName);
  }

  // ---------------------------------------------------------------- node


  /**
   * Returns value of first available child text node or <code>null</code> if not found.
   */
  public static String getFirstChildTextNodeValue(Node node) {
    NodeList children = node.getChildNodes();
    int len = children.getLength();
    for (int i = 0; i < len; i++) {
      Node n = children.item(i);
      if (n.getNodeType() == Node.TEXT_NODE) {
        return n.getNodeValue();
      }
    }
    return null;
  }

  /**
   * Returns value of single child text node or <code>null</code>.
   */
  public static String getChildTextNodeValue(Node node) {
    if (node.getChildNodes().getLength() != 1) {
      return null;
    }
    Node item0 = node.getChildNodes().item(0);
    if (item0.getNodeType() != Node.TEXT_NODE) {
      return null;
    }
    return item0.getNodeValue();
  }



}

   
    
  
Related examples in the same category
1.Create an XML document and search by XPath
2.Use XPath to select node
3.Shallow print of node list
4.Deep print of node list
5.Parse with XPath
6.Get the String data associated with the XPath selection supplied
7.This program evaluates XPath expressions
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.