List of usage examples for org.w3c.dom Element getNodeType
public short getNodeType();
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<Services><service name='qwerty' id=''><File rootProfile='abcd' extension='acd'><Columns>" + "<name id='0' profileName='DATE' type='java'></name><name id='1' profileName='DATE' type='java'></name>" + "</Columns></File><File rootProfile='efg' extension='ghi'><Columns><name id='a' profileName='DATE' type='java'></name>" + "<name id='b' profileName='DATE' type='java'></name></Columns></File></service></Services>"; DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = null; documentBuilder = documentFactory.newDocumentBuilder(); org.w3c.dom.Document doc = documentBuilder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes()))); doc.getDocumentElement().normalize(); NodeList nodeList0 = doc.getElementsByTagName("service"); NodeList nodeList1 = null;// w w w. ja v a 2s.com NodeList nodeList2 = null; System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) { Node node0 = nodeList0.item(temp0); System.out.println("\nElement type :" + node0.getNodeName()); Element Service = (Element) node0; if (node0.getNodeType() != Node.ELEMENT_NODE) { continue; } System.out.println("name : " + Service.getAttribute("name")); System.out.println("id : " + Service.getAttribute("id")); nodeList1 = Service.getChildNodes(); for (int temp = 0; temp < nodeList1.getLength(); temp++) { Node node1 = nodeList1.item(temp); System.out.println("\nElement type :" + node1.getNodeName()); Element File = (Element) node1; if (node1.getNodeType() != Node.ELEMENT_NODE) { continue; } System.out.println("rootProfile:" + File.getAttribute("rootProfile")); System.out.println("extension : " + File.getAttribute("extension")); nodeList2 = File.getChildNodes();// colums for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) { Element column = (Element) nodeList2.item(temp1); NodeList nodeList4 = column.getChildNodes(); for (int temp3 = 0; temp3 < nodeList4.getLength(); temp3++) { Element name = (Element) nodeList4.item(temp3); if (name.getNodeType() != Node.ELEMENT_NODE) { continue; } System.out.println("id:" + name.getAttribute("id")); System.out.println("profileName : " + name.getAttribute("profileName")); System.out.println("type : " + name.getAttribute("type")); } } } } }
From source file:Main.java
public static String getAttributeValue(Element start, String attrName) { if (start.getNodeType() == Element.ELEMENT_NODE) { NamedNodeMap startAttr = start.getAttributes(); for (int i = 0; i < startAttr.getLength(); i++) { Node attr = startAttr.item(i); if (attrName.equals(attr.getNodeName().trim())) return attr.getNodeValue().trim(); }//from w w w.java 2s . co m return null; } else return null; }
From source file:Main.java
private static String getTextValue(String def, Element doc, String tag) { String value = def;/* w w w. java2 s .c o m*/ NodeList noteList = doc.getElementsByTagName(tag); if (noteList.getLength() > 0) { for (int i = 0; i < noteList.getLength(); i++) { Node note = noteList.item(i); Element noteElement = (Element) note; if (noteElement.getNodeType() == Node.ELEMENT_NODE) { if (noteElement.getElementsByTagName("pitch").getLength() > 0) { Element pitchElement = (Element) noteElement.getElementsByTagName("pitch").item(0); System.out.println("Staff idX : " + pitchElement.getElementsByTagName("step").item(0).getTextContent()); } else { System.out.println("yoktir"); } } else { System.out.println("not element"); } } // value = nl.item(0).getFirstChild().getNodeValue(); } return value; }
From source file:Main.java
private static String getTextValue(String def, Element doc, String tag) { String value = def;/* w w w . j ava2 s . co m*/ NodeList noteList = doc.getElementsByTagName(tag); if (noteList.getLength() > 0) { for (int i = 0; i < noteList.getLength(); i++) { Node note = noteList.item(i); Element noteElement = (Element) note; if (noteElement.getNodeType() == Node.ELEMENT_NODE) { if (noteElement.getElementsByTagName("pitch").getLength() > 0) { Element pitchElement = (Element) noteElement.getElementsByTagName("pitch").item(0); System.out.println("Staff idX : " + pitchElement.getElementsByTagName("step").item(0).getTextContent()); } else { System.out.println("yoktir"); } // System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); // System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); } else { System.out.println("not element"); } } // value = nl.item(0).getFirstChild().getNodeValue(); } return value; }
From source file:Main.java
public static boolean updateTextNode(String nodeName, Element searchFrom, String data, int position) throws Exception { boolean result = false; Element currentElement = (Element) searchFrom.getElementsByTagName(nodeName).item(position); if (currentElement != null && currentElement.getNodeType() == Node.ELEMENT_NODE) { Text textNode = (Text) (currentElement.getFirstChild()); textNode.setData(data);//w w w . j a v a 2s. com if (textNode != null && textNode.getNodeValue() == null) result = false; else result = true; } return result; }
From source file:Main.java
/** * equivalent to the XPath expression './/tagName[@attrName='attrValue']' *///from w w w . ja va2s . co m public static Element getElementByAttributeValue(Node start, String tagName, String attrName, String attrValue) { NodeList nl = ((Element) start).getElementsByTagName(tagName); int l = nl.getLength(); if (l == 0) { return null; } Element e = null; String compareValue = null; for (int i = 0; i < l; i++) { e = (Element) nl.item(i); if (e.getNodeType() == Node.ELEMENT_NODE) { compareValue = e.getAttribute(attrName); if (compareValue.equals(attrValue)) { return e; } } } return null; }
From source file:Main.java
/** * equivalent to the XPath expression './/tagName[@attrName='attrValue']' *//*ww w . j a v a 2 s . c om*/ public static Element getElementByAttributeValue(final Node start, final String tagName, final String attrName, final String attrValue) { NodeList nl = ((Element) start).getElementsByTagName(tagName); int l = nl.getLength(); if (l == 0) { return null; } Element e = null; String compareValue = null; for (int i = 0; i < l; i++) { e = (Element) nl.item(i); if (e.getNodeType() == Node.ELEMENT_NODE) { compareValue = e.getAttribute(attrName); if (compareValue.equals(attrValue)) { return e; } } } return null; }
From source file:Main.java
public static ArrayList<String> getNodeListAttValAsStringCol(final String xPath, final Node node, final String attrName) throws Exception { ArrayList<String> retV = new ArrayList<String>(); NodeList nl = getNodesListXpathNode(xPath, node); int l = nl.getLength(); Element e = null; String val = ""; for (int i = 0; i < l; i++) { e = (Element) nl.item(i); if (e.getNodeType() == Node.ELEMENT_NODE) { val = e.getAttribute(attrName); if (val != null && val.length() > 0) { retV.add(val); }//from ww w.j av a2 s . c om } } return retV; }
From source file:Main.java
public static ArrayList<String> getNodeListAttValAsStringCol(String Xpath, Node node, String attrName) throws Exception { ArrayList<String> retV = new ArrayList<String>(); NodeList nl = getNodesListXpathNode(Xpath, node); int l = nl.getLength(); Element e = null; String val = ""; for (int i = 0; i < l; i++) { e = (Element) nl.item(i); if (e.getNodeType() == Node.ELEMENT_NODE) { val = e.getAttribute(attrName); if (val != null && val.length() > 0) { //log.info("getNodeListAttValAsStringCol val = "+val +" attname = "+attrName); /*try {/* w ww . j a v a 2s. co m*/ log.info(convertToStringLeaveCDATA(e)); }catch(Exception E) { E.printStackTrace(); }*/ retV.add(val); } } } return retV; }
From source file:Main.java
public static ArrayList<String> getNodeListAttValAsStringCols(String Xpath, Node node, String[] attrNames, String sep) throws Exception { ArrayList<String> retV = new ArrayList<String>(); if (sep == null) { sep = " "; }/* w w w. jav a2 s. c o m*/ int aNamesL = attrNames.length; if (aNamesL > 0) { NodeList nl = getNodesListXpathNode(Xpath, node); int l = nl.getLength(); Element e = null; String val = ""; for (int i = 0; i < l; i++) { e = (Element) nl.item(i); if (e.getNodeType() == Node.ELEMENT_NODE) { StringBuilder sb = new StringBuilder(); for (int y = 0; y < aNamesL; y++) { sb.append(e.getAttribute(attrNames[y])); if (y < aNamesL - 1) { sb.append(sep); } } val = sb.toString(); if (val != null && val.length() > 0) { retV.add(val); } } } } return retV; }