Example usage for org.w3c.dom Node ELEMENT_NODE

List of usage examples for org.w3c.dom Node ELEMENT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node ELEMENT_NODE.

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

public static List<Map<String, String>> ReadPlaylistItemsFromFile(String path, String filename) {

    List<Map<String, String>> results = new ArrayList<Map<String, String>>();

    try {/*from   ww w.j a  va  2 s.  c o m*/
        File fXmlFile = new File(path, filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList songList = doc.getElementsByTagName("song");
        Log.d("ReadItemsFromFile", "List Length: " + songList.getLength());

        for (int i = 0; i < songList.getLength(); i++) {

            Node nNode = songList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                HashMap<String, String> mapElement = new HashMap<String, String>();

                mapElement.put("name", eElement.getElementsByTagName("name").item(0).getTextContent());
                mapElement.put("artist", eElement.getElementsByTagName("artist").item(0).getTextContent());
                mapElement.put("startTime",
                        eElement.getElementsByTagName("startTime").item(0).getTextContent());
                mapElement.put("url", eElement.getElementsByTagName("url").item(0).getTextContent());

                results.add(mapElement);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return results;

}

From source file:Main.java

/**
 * Checks to see if parent node has more of these nodes looks at tagName
 * //from  w w w .  j av a 2  s.c  om
 * @param node
 * @return
 */
private static final boolean parentNodeHasMoreOfThese(Element node) {
    int count = 0;
    NodeList children = node.getParentNode().getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getTagName().equals(((Element) child).getTagName())) {
                count++;
                if (count > 1)
                    return true;
            }
        }
    }

    return false;
}

From source file:Main.java

public static void spreadNamespaces(Node node, String tns, boolean overwrite) {
    Document doc = node instanceof Document ? (Document) node : node.getOwnerDocument();
    boolean isParent = false;
    while (node != null) {
        Node next = null;//from   www .j a v a2  s.  c  o  m
        if (!isParent && node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNamespaceURI() == null) {
                node = doc.renameNode(node, tns, node.getNodeName());
            } else {
                if (overwrite) {
                    tns = node.getNamespaceURI();
                }
            }
            NamedNodeMap nodeMap = node.getAttributes();
            int nodeMapLengthl = nodeMap.getLength();
            for (int i = 0; i < nodeMapLengthl; i++) {
                Node attr = nodeMap.item(i);
                if (attr.getNamespaceURI() == null) {
                    doc.renameNode(attr, tns, attr.getNodeName());
                }
            }
        }
        isParent = (isParent || (next = node.getFirstChild()) == null)
                && (next = node.getNextSibling()) == null;
        node = isParent ? node.getParentNode() : next;
        if (isParent && node != null) {
            if (overwrite) {
                tns = node.getNamespaceURI();
            }
        }
    }
}

From source file:Main.java

/**
 * Gets a TEXT node value from the specified Element node.
 *
 * @param element a ELEMENT Node has TEXT node 
 *//*  w  ww.j  av  a2s .  c  om*/
/* TODO: Child? */
public static String getTextData(Node element) {
    if (element.getNodeType() != Node.ELEMENT_NODE) {
        throw new IllegalArgumentException(element + " is not ELEMENT node");
    }
    Node description = element.getNextSibling();
    if (description.getNodeType() != Node.TEXT_NODE) {
        throw new IllegalArgumentException(description + " is not TEXT node");
    }
    return description.getNodeValue();
}

From source file:Main.java

/**
 * Update a property of a given configuration file and return a string representation of the
 * xml document//from  w w  w .j  av a 2 s . co m
 * @param doc
 * @param property
 * @param newValue
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 * @throws ClassNotFoundException 
 */
public static String updateXmlDoc(Document doc, List<String> properties)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    for (String property : properties) {
        String key = property.split(":")[0];
        String value = property.split(":")[1];
        NodeList propertiesNode = doc.getElementsByTagName("esf:property");
        for (int i = 0; i < propertiesNode.getLength(); i++) {
            Node node = propertiesNode.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element el = (Element) node;
                if (key.equals(el.getAttribute("name"))) {
                    el.getElementsByTagName("esf:value").item(0).setTextContent(value);
                }
            }
        }
    }
    return parseXmlDocToString(doc);
}

From source file:Main.java

public static void ReadXMLFile()

{

    try {//  www. j a  va 2  s  .  c om

        File fXmlFile = new File("D:\\FAR_Documents\\__Startamap\\Home.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("Staff id : " + eElement.getAttribute("id"));
                System.out.println(
                        "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                System.out.println(
                        "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                System.out.println(
                        "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                System.out.println(
                        "Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *///w w w. j a v a 2  s  .  c om
public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if ((target == null) || (node.getOwnerDocument() == target)) {
        // same Document
        return node.cloneNode(deep);
    } else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null) {
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }
            }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep) {
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                newNode.appendChild(cloneNode(child, target, true));
            }
        }

        return newNode;
    }
}

From source file:Main.java

/**
 * This method will find all the parameters under this <code>paramsElement</code> and return them as
 * Map<String, String>. For example,
 * <pre>//from  www  .  j  a  v a  2  s  .c  o  m
 *   <result ... >
 *      <param name="param1">value1</param>
 *      <param name="param2">value2</param>
 *      <param name="param3">value3</param>
 *   </result>
 * </pre>
 * will returns a Map<String, String> with the following key, value pairs :-
 * <ul>
 *  <li>param1 - value1</li>
 *  <li>param2 - value2</li>
 *  <li>param3 - value3</li>
 * </ul>
 *
 * @param paramsElement
 * @return
 */
public static Map getParams(Element paramsElement) {
    LinkedHashMap params = new LinkedHashMap();

    if (paramsElement == null) {
        return params;
    }

    NodeList childNodes = paramsElement.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);

        if ((childNode.getNodeType() == Node.ELEMENT_NODE) && "param".equals(childNode.getNodeName())) {
            Element paramElement = (Element) childNode;
            String paramName = paramElement.getAttribute("name");

            String val = getContent(paramElement);
            if (val.length() > 0) {
                params.put(paramName, val);
            }
        }
    }
    return params;
}

From source file:Main.java

private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    if (rootNode == exclude) {
        return;/*from  ww w. j  a v  a 2  s  .c om*/
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = el.getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
                    r = r.getNextSibling();
                }
                if (r == null) {
                    return;
                }
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
}

From source file:Main.java

public static List<Node> getChildElements(Node node) {
    NodeList children = node.getChildNodes();
    int childCount = children.getLength();
    List<Node> nodes = new ArrayList<Node>(childCount);
    for (int i = 0; i < childCount; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            nodes.add(child);//  w w  w. j  a va2s .c o  m
        }
    }
    return nodes;
}