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);
}
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);
}
}
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);
}
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();
}
}
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;
}
Aggregations