Search in sources :

Example 21 with Node

use of org.w3c.dom.Node in project camel by apache.

the class XmlLineNumberParserTest method testParse.

public void testParse() throws Exception {
    FileInputStream fis = new FileInputStream("src/test/resources/org/apache/camel/util/camel-context.xml");
    Document dom = XmlLineNumberParser.parseXml(fis);
    assertNotNull(dom);
    NodeList list = dom.getElementsByTagName("beans");
    assertEquals(1, list.getLength());
    Node node = list.item(0);
    String lineNumber = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER);
    String lineNumberEnd = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
    assertEquals("23", lineNumber);
    assertEquals("48", lineNumberEnd);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream)

Example 22 with Node

use of org.w3c.dom.Node in project groovy by apache.

the class DOMCategory method appendNodes.

private static void appendNodes(Node self, Closure c) {
    Node parent = self.getParentNode();
    Node beforeNode = self.getNextSibling();
    DOMBuilder b = new DOMBuilder(self.getOwnerDocument());
    Element newNodes = (Element) b.invokeMethod("rootNode", c);
    Iterator<Node> iter = XmlGroovyMethods.iterator(children(newNodes));
    while (iter.hasNext()) {
        parent.insertBefore(iter.next(), beforeNode);
    }
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DOMBuilder(groovy.xml.DOMBuilder)

Example 23 with Node

use of org.w3c.dom.Node in project groovy by apache.

the class DOMCategory method setValue.

public static void setValue(Element self, String value) {
    Node firstChild = self.getFirstChild();
    if (firstChild == null) {
        firstChild = self.getOwnerDocument().createTextNode(value);
        self.appendChild(firstChild);
    }
    firstChild.setNodeValue(value);
}
Also used : Node(org.w3c.dom.Node)

Example 24 with Node

use of org.w3c.dom.Node in project hadoop by apache.

the class HostsFileReader method readXmlFileToMapWithFileInputStream.

public static void readXmlFileToMapWithFileInputStream(String type, String filename, InputStream fileInputStream, Map<String, Integer> map) throws IOException {
    Document dom;
    DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = builder.newDocumentBuilder();
        dom = db.parse(fileInputStream);
        // Examples:
        // <host><name>host1</name></host>
        // <host><name>host2</name><timeout>123</timeout></host>
        // <host><name>host3</name><timeout>-1</timeout></host>
        // <host><name>host4, host5,host6</name><timeout>1800</timeout></host>
        Element doc = dom.getDocumentElement();
        NodeList nodes = doc.getElementsByTagName("host");
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) node;
                // Support both single host and comma-separated list of hosts.
                String v = readFirstTagValue(e, "name");
                String[] hosts = StringUtils.getTrimmedStrings(v);
                String str = readFirstTagValue(e, "timeout");
                Integer timeout = (str == null) ? null : Integer.parseInt(str);
                for (String host : hosts) {
                    map.put(host, timeout);
                    LOG.info("Adding a node \"" + host + "\" to the list of " + type + " hosts from " + filename);
                }
            }
        }
    } catch (IOException | SAXException | ParserConfigurationException e) {
        LOG.fatal("error parsing " + filename, e);
        throw new RuntimeException(e);
    } finally {
        fileInputStream.close();
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 25 with Node

use of org.w3c.dom.Node in project hadoop by apache.

the class WebServicesTestUtils method getXmlString.

public static String getXmlString(Element element, String name) {
    NodeList id = element.getElementsByTagName(name);
    Element line = (Element) id.item(0);
    if (line == null) {
        return null;
    }
    Node first = line.getFirstChild();
    // handle empty <key></key>
    if (first == null) {
        return "";
    }
    String val = first.getNodeValue();
    if (val == null) {
        return "";
    }
    return val;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node)

Aggregations

Node (org.w3c.dom.Node)2347 NodeList (org.w3c.dom.NodeList)1062 Element (org.w3c.dom.Element)720 Document (org.w3c.dom.Document)545 NamedNodeMap (org.w3c.dom.NamedNodeMap)333 ArrayList (java.util.ArrayList)318 DocumentBuilder (javax.xml.parsers.DocumentBuilder)202 IOException (java.io.IOException)176 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)147 Test (org.junit.Test)132 HashMap (java.util.HashMap)127 Attr (org.w3c.dom.Attr)126 SAXException (org.xml.sax.SAXException)107 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)98 HashSet (java.util.HashSet)86 InputSource (org.xml.sax.InputSource)75 XPath (javax.xml.xpath.XPath)70 List (java.util.List)67 File (java.io.File)62 ByteArrayInputStream (java.io.ByteArrayInputStream)57