List of usage examples for org.w3c.dom Element getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuild = dbf.newDocumentBuilder(); Document doc = docBuild.parse(new File("test2.xml")); Element x = doc.getDocumentElement(); NodeList m = x.getChildNodes(); for (int i = 0; i < m.getLength(); i++) { Node it = m.item(i);//from www .ja va 2 s .c o m if (it.getNodeType() == 3) { System.out.println(it.getNodeValue()); } } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from w ww. ja v a 2s .com*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; for (int i = 0; i < children.getLength(); i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { rootElement.removeChild(element); } } } System.out.println(doc.getDocumentElement()); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//ww w. ja v a 2 s . c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; int count = children.getLength(); for (int i = 0; i < count; i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { element.setAttribute("showPageNumbers", "no"); } } } System.out.println(doc.getDocumentElement()); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from ww w . j av a 2 s.com*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; int count = children.getLength(); for (int i = 0; i < count; i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { // Get the list of <tocEntry> items NodeList tocitems = element.getElementsByTagName("tocEntry"); // Obtain a reference to the second one Node secondChild = tocitems.item(1); // Create a new <tocEntry> element Element newTOCItem = doc.createElement("tocEntry"); // Create a new "Help" text node Text newText = doc.createTextNode("Help"); // Make it a child of the new <tocEntry> element // <tocEntry>Help</tocEntry> newTOCItem.appendChild(newText); // Add the new <tocEntry> element to <tableOfContents> element.insertBefore(newTOCItem, secondChild); } } } System.out.println(doc.getDocumentElement()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/* w ww . j ava 2s .co m*/ DocumentBuilder db = dbf.newDocumentBuilder(); File file1 = new File("input1.xml"); Document doc1 = db.parse(file1); Element rootElement1 = doc1.getDocumentElement(); File file2 = new File("input2.xml"); Document doc2 = db.parse(file2); Element rootElement2 = doc2.getDocumentElement(); // Copy Child Nodes NodeList childNodes2 = rootElement2.getChildNodes(); for (int x = 0; x < childNodes2.getLength(); x++) { Node importedNode = doc1.importNode(childNodes2.item(x), true); if (importedNode.getNodeType() == Node.ELEMENT_NODE) { Element importedElement = (Element) importedNode; // Copy Attributes NamedNodeMap namedNodeMap2 = rootElement2.getAttributes(); for (int y = 0; y < namedNodeMap2.getLength(); y++) { Attr importedAttr = (Attr) doc1.importNode(namedNodeMap2.item(y), true); importedElement.setAttributeNodeNS(importedAttr); } } rootElement1.appendChild(importedNode); } // Output Document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(doc1); StreamResult result = new StreamResult(System.out); t.transform(source, result); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = Factory.newDocumentBuilder(); Document doc = builder.parse("myxml.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//" + "item1" + "/*"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { Element el = (Element) nodes.item(i); System.out.println("tag: " + el.getNodeName()); if (el.getFirstChild().getNodeType() == Node.TEXT_NODE) System.out.println("inner value:" + el.getFirstChild().getNodeValue()); NodeList children = el.getChildNodes(); for (int k = 0; k < children.getLength(); k++) { Node child = children.item(k); if (child.getNodeType() != Node.TEXT_NODE) { System.out.println("child tag: " + child.getNodeName()); if (child.getFirstChild().getNodeType() == Node.TEXT_NODE) System.out.println("inner child value:" + child.getFirstChild().getNodeValue()); }/*from ww w. j ava2 s .co m*/ } } }
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;//from w w w .j a v a2s. c o m 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 void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("testrss.xml")); Element root = doc.getDocumentElement(); if (!root.getTagName().equalsIgnoreCase("rss")) { throw new IOException("Invalid RSS document"); }/*ww w .j a va 2 s. c o m*/ List<RSSChannel> channels = readChannels(root.getChildNodes()); for (RSSChannel channel : channels) { System.out.println("Channel: "); System.out.println(" title: " + channel.getTitle()); System.out.println(" link: " + channel.getLink()); System.out.println(" description: " + channel.getDescription()); for (RSSItem item : channel.getItems()) { System.out.println(" Item: "); System.out.println(" title: " + item.getTitle()); System.out.println(" link: " + item.getLink()); System.out.println(" description: " + item.getDescription()); System.out.println(" pubDate: " + item.getPubDate()); System.out.println(" guid: " + item.getGuid()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w ww . ja v a 2 s. c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); File file1 = new File("input1.xml"); Document doc1 = db.parse(file1); Element rootElement1 = doc1.getDocumentElement(); File file2 = new File("input2.xml"); Document doc2 = db.parse(file2); Element rootElement2 = doc2.getDocumentElement(); // Copy Attributes NamedNodeMap namedNodeMap2 = rootElement2.getAttributes(); for (int x = 0; x < namedNodeMap2.getLength(); x++) { Attr importedNode = (Attr) doc1.importNode(namedNodeMap2.item(x), true); rootElement1.setAttributeNodeNS(importedNode); } // Copy Child Nodes NodeList childNodes2 = rootElement2.getChildNodes(); for (int x = 0; x < childNodes2.getLength(); x++) { Node importedNode = doc1.importNode(childNodes2.item(x), true); rootElement1.appendChild(importedNode); } // Output Document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(doc1); StreamResult result = new StreamResult(System.out); t.transform(source, result); }
From source file:client.QueryLastFm.java
License:asdf
public static void main(String[] args) throws Exception { // isAlreadyInserted("asdfs","jas,jnjkah"); // FileWriter fw = new FileWriter(".\\tracks.csv"); OutputStream track_os = new FileOutputStream(".\\tracks.csv"); PrintWriter out = new PrintWriter(new OutputStreamWriter(track_os, "UTF-8")); OutputStream track_id_os = new FileOutputStream(".\\track_id_sim_track_id.csv"); PrintWriter track_id_out = new PrintWriter(new OutputStreamWriter(track_id_os, "UTF-8")); track_id_out.print(""); ByteArrayInputStream input;//from w w w.ja v a 2 s . c om Document doc = null; CloseableHttpClient httpclient = HttpClients.createDefault(); String trackName = ""; String artistName = ""; String sourceMbid = ""; out.print("ID");// first row first column out.print(","); out.print("TrackName");// first row second column out.print(","); out.println("Artist");// first row third column track_id_out.print("source");// first row second column track_id_out.print(","); track_id_out.println("target");// first row third column // track_id_out.print(","); // track_id_out.println("type");// first row third column // out.flush(); // out.close(); // fw.close(); // os.close(); try { URI uri = new URIBuilder().setScheme("http").setHost("ws.audioscrobbler.com").setPath("/2.0/") .setParameter("method", "track.getsimilar").setParameter("artist", "cher") .setParameter("track", "believe").setParameter("limit", "100") .setParameter("api_key", "88858618961414f8bec919bddd057044").build(); // new URIBuilder(). HttpGet request = new HttpGet(uri); // request. // This is useful for last.fm logging and preventing them from blocking this client request.setHeader(HttpHeaders.USER_AGENT, "nileshmore@gatech.edu - ClassAssignment at GeorgiaTech Non-commercial use"); HttpGet httpGet = new HttpGet( "http://ws.audioscrobbler.com/2.0/?method=track.getsimilar&artist=cher&track=believe&limit=4&api_key=88858618961414f8bec919bddd057044"); CloseableHttpResponse response = httpclient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // The underlying HTTP connection is still held by the response object // to allow the response content to be streamed directly from the network socket. // In order to ensure correct deallocation of system resources // the user MUST call CloseableHttpResponse#close() from a finally clause. // Please note that if response content is not fully consumed the underlying // connection cannot be safely re-used and will be shut down and discarded // by the connection manager. try { if (statusCode == 200) { HttpEntity entity1 = response.getEntity(); BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()))); Document document = builder.parse((response.getEntity().getContent())); Element root = document.getDocumentElement(); root.normalize(); // Need to focus and resolve this part NodeList nodes; nodes = root.getChildNodes(); nodes = root.getElementsByTagName("track"); if (nodes.getLength() == 0) { // System.out.println("empty"); return; } Node trackNode; for (int k = 0; k < nodes.getLength(); k++) // can access all tracks now { trackNode = nodes.item(k); NodeList trackAttributes = trackNode.getChildNodes(); // check if mbid is present in track attributes // System.out.println("Length " + (trackAttributes.item(5).getNodeName().compareToIgnoreCase("mbid") == 0)); if ((trackAttributes.item(5).getNodeName().compareToIgnoreCase("mbid") == 0)) { if (((Element) trackAttributes.item(5)).hasChildNodes()) ;// System.out.println("Go aHead"); else continue; } else continue; for (int n = 0; n < trackAttributes.getLength(); n++) { Node attribute = trackAttributes.item(n); if ((attribute.getNodeName().compareToIgnoreCase("name")) == 0) { // System.out.println(((Element)attribute).getFirstChild().getNodeValue()); trackName = ((Element) attribute).getFirstChild().getNodeValue(); // make string encoding as UTF-8 ************ } if ((attribute.getNodeName().compareToIgnoreCase("mbid")) == 0) { // System.out.println(n + " " + ((Element)attribute).getFirstChild().getNodeValue()); sourceMbid = attribute.getFirstChild().getNodeValue(); } if ((attribute.getNodeName().compareToIgnoreCase("artist")) == 0) { NodeList ArtistNodeList = attribute.getChildNodes(); for (int j = 0; j < ArtistNodeList.getLength(); j++) { Node Artistnode = ArtistNodeList.item(j); if ((Artistnode.getNodeName().compareToIgnoreCase("name")) == 0) { // System.out.println(((Element)Artistnode).getFirstChild().getNodeValue()); artistName = ((Element) Artistnode).getFirstChild().getNodeValue(); } } } } out.print(sourceMbid); out.print(","); out.print(trackName); out.print(","); out.println(artistName); // out.print(","); findSimilarTracks(track_id_out, sourceMbid, trackName, artistName); } track_id_out.flush(); out.flush(); out.close(); track_id_out.close(); track_os.close(); // fw.close(); Element trac = (Element) nodes.item(0); // trac.normalize(); nodes = trac.getChildNodes(); // System.out.println(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); // System.out.println(node.getNodeName()); if ((node.getNodeName().compareToIgnoreCase("name")) == 0) { // System.out.println(((Element)node).getFirstChild().getNodeValue()); } if ((node.getNodeName().compareToIgnoreCase("mbid")) == 0) { // System.out.println(((Element)node).getFirstChild().getNodeValue()); } if ((node.getNodeName().compareToIgnoreCase("artist")) == 0) { // System.out.println("Well"); NodeList ArtistNodeList = node.getChildNodes(); for (int j = 0; j < ArtistNodeList.getLength(); j++) { Node Artistnode = ArtistNodeList.item(j); if ((Artistnode.getNodeName().compareToIgnoreCase("name")) == 0) { /* System.out.println(((Element)Artistnode).getFirstChild().getNodeValue());*/ } /*System.out.println(Artistnode.getNodeName());*/ } } } /*if(node instanceof Element){ //a child element to process Element child = (Element) node; String attribute = child.getAttribute("width"); }*/ // System.out.println(root.getAttribute("status")); NodeList tracks = root.getElementsByTagName("track"); Element track = (Element) tracks.item(0); // System.out.println(track.getTagName()); track.getChildNodes(); } else { System.out.println("failed with status" + response.getStatusLine()); } // input = (ByteArrayInputStream)entity1.getContent(); // do something useful with the response body // and ensure it is fully consumed } finally { response.close(); } } finally { System.out.println("Exited succesfully."); httpclient.close(); } }