List of usage examples for org.w3c.dom Document hasChildNodes
public boolean hasChildNodes();
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder.parse(new File("data.xml")); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); if (doc.hasChildNodes()) { printNote(doc.getChildNodes(), 1); }/*from ww w .j av a 2 s. c o m*/ }
From source file:Main.java
public static void ReadXMLFile2() { try {// w w w.j ava2 s .c om File file = new File("D:\\FAR_Documents\\__Startamap\\Home.xml"); DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder.parse(file); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); if (doc.hasChildNodes()) { printNote(doc.getChildNodes()); } } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:Main.java
/** * Transform XML to JSON//from ww w . j a v a2s . com * * @param xml * @return * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static String toJson(String xml) throws ParserConfigurationException, SAXException, IOException { JsonObject rootJson = new JsonObject(); DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(new StringReader(xml))); if (doc.hasChildNodes()) { traverseNode(doc, rootJson, null); } Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); String json = gson.toJson(rootJson); return json; }
From source file:cz.tyr.android.currencyrates.bank.RaiffeisenBank.java
@Override public int downloadData() { if (mCurrency == null) { mCurrency = getDefaultCurrencyValue(); }/*w ww . ja v a2 s. c om*/ String dateStr = null; String rateStr = null; String url = getDataUrl(); if (DEBUG > 0) { Log.d(TAG, "Download data for RB_CZ"); Log.d(TAG, " * url = : " + url); Log.d(TAG, " * currency = " + mCurrency); Log.d(TAG, " * exchange = " + mExchangeType); Log.d(TAG, " * direction = " + mExchangeDirection); } HttpClient sClient = new DefaultHttpClient(); HttpGet request = new HttpGet(url); InputStream stream = null; try { stream = sClient.execute(request).getEntity().getContent(); } catch (IOException e) { Log.d(TAG, "Problem downloading the XML data."); return 1; } try { if (DEBUG > 1) Log.d(TAG, " - Factory start"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc = dbf.newDocumentBuilder().parse(stream); if (DEBUG > 1) Log.d(TAG, " - Factory end"); if (doc != null && doc.hasChildNodes()) { if (DEBUG > 1) Log.d(TAG, " - Parse start"); // find the root element for (int i = 0; i < doc.getChildNodes().getLength(); i++) { Node root = doc.getChildNodes().item(i); if (root.getNodeType() == Node.ELEMENT_NODE || root.getNodeName().equals("exchange_rates")) { NodeList list = doc.getChildNodes().item(i).getChildNodes(); // find first node for (int j = 0; j < list.getLength(); j++) { Node n = list.item(j); // check the attributes of this element if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals("exchange_rate")) { boolean go = false; if (DEBUG > 1) Log.d(TAG, " # Got EXCHANGE_RATE element!"); for (int k = 0; k < n.getAttributes().getLength(); k++) { Node a = n.getAttributes().item(k); if (a.getNodeName().equals("type") && a.getNodeValue().equals( "XML_RATE_TYPE_EBNK_" + mExchangeDirection + "_" + mExchangeType)) { if (DEBUG > 1) Log.d(TAG, " - CORRECT ELEMENT! TAKE THE DATE!"); go = true; } else if (go && a.getNodeName().equals("valid_from")) { if (DEBUG > 1) Log.d(TAG, " - GOT DATE! " + a.getNodeValue()); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); Date fdate = (Date) formatter.parse(a.getNodeValue()); formatter = new SimpleDateFormat( mResources.getString(R.string.date_time_format)); dateStr = formatter.format(fdate); // stop the loop break; } } // if it is correct element, go for the rate if (go) { if (DEBUG > 1) Log.d(TAG, " - Searching for the rate!"); NodeList currencies = n.getChildNodes(); // check the attributes for (int k = 0; k < currencies.getLength(); k++) { Node c = currencies.item(k); if (c.getNodeType() == Node.ELEMENT_NODE && c.getNodeName().equals("currency")) { boolean bool = false; String rateTmp = null; for (int l = 0; l < c.getAttributes().getLength(); l++) { Node a = c.getAttributes().item(l); if (a.getNodeName().equals("name") && a.getNodeValue().equals(mCurrency)) { if (DEBUG > 1) Log.d(TAG, " -- Got the Currency!!!"); bool = true; } else if (a.getNodeName().equals("rate")) { if (DEBUG > 1) Log.d(TAG, " -- Got the RATE!!!" + a.getNodeValue()); rateTmp = a.getNodeValue(); } if (bool && rateTmp != null) { rateStr = rateTmp; if (DEBUG > 1) Log.d(TAG, " -- Got the Currency VALUE: " + rateStr); // stop the loop break; } } } // stop the loop if (rateStr != null) { break; } } } } // stop the loop if (rateStr != null) { break; } } } // stop the loop if (rateStr != null) { break; } } if (DEBUG > 1) Log.d(TAG, " - Parse end"); } } catch (Exception e) { e.printStackTrace(); return 1; } // Check the values if (dateStr == null || rateStr == null) { Log.d(TAG, " # One of the values is null!"); return 1; } setCurrencyDate(dateStr); setCurrencyRate(Float.parseFloat(rateStr)); return 0; }
From source file:cz.tyr.android.currencyrates.bank.UniCreditBank.java
@Override public int downloadData() { if (mCurrency == null) { mCurrency = getDefaultCurrencyValue(); }/*from w ww . ja va 2s . co m*/ String dateStr = null; String rateStr = null; String url = getDataUrl(); if (DEBUG > 0) { Log.d(TAG, "Download data for UCB_CZ"); Log.d(TAG, " * url = : " + url); Log.d(TAG, " * currency = " + mCurrency); Log.d(TAG, " * exchange = " + mExchangeType); Log.d(TAG, " * direction = " + mExchangeDirection); } HttpClient sClient = new DefaultHttpClient(); HttpGet request = new HttpGet(url); InputStream stream = null; try { stream = sClient.execute(request).getEntity().getContent(); } catch (IOException e) { Log.d(TAG, "Problem downloading the XML data."); return 1; } try { if (DEBUG > 1) Log.d(TAG, " - Factory start"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc = dbf.newDocumentBuilder().parse(stream); if (DEBUG > 1) Log.d(TAG, " - Factory end"); if (doc != null && doc.hasChildNodes()) { if (DEBUG > 1) Log.d(TAG, " - Parse start"); // find the root element for (int i = 0; i < doc.getChildNodes().getLength(); i++) { Node root = doc.getChildNodes().item(i); if (root.getNodeType() == Node.ELEMENT_NODE || root.getNodeName().equals("exchange_rates")) { NodeList list = doc.getChildNodes().item(i).getChildNodes(); // find first node for (int j = 0; j < list.getLength(); j++) { Node n = list.item(j); // check the attributes of this element if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals("exchange_rate")) { boolean go = false; if (DEBUG > 1) Log.d(TAG, " # Got EXCHANGE_RATE element!"); for (int k = 0; k < n.getAttributes().getLength(); k++) { Node a = n.getAttributes().item(k); if (a.getNodeName().equals("type") && a.getNodeValue().equals( "XML_RATE_TYPE_UCB_" + mExchangeDirection + "_" + mExchangeType)) { if (DEBUG > 1) Log.d(TAG, " - CORRECT ELEMENT! TAKE THE DATE!"); go = true; } else if (go && a.getNodeName().equals("valid_from")) { if (DEBUG > 1) Log.d(TAG, " - GOT DATE! " + a.getNodeValue()); DateFormat formatter = new SimpleDateFormat("HH:mm"); String time = formatter.format(new Date()); formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); Date fdate = (Date) formatter.parse(a.getNodeValue() + " " + time); formatter = new SimpleDateFormat( mResources.getString(R.string.date_time_format)); dateStr = formatter.format(fdate); // stop the loop break; } } // if it is correct element, go for the rate if (go) { if (DEBUG > 1) Log.d(TAG, " - Searching for the rate!"); NodeList currencies = n.getChildNodes(); // check the attributes for (int k = 0; k < currencies.getLength(); k++) { Node c = currencies.item(k); if (c.getNodeType() == Node.ELEMENT_NODE && c.getNodeName().equals("currency")) { boolean bool = false; String rateTmp = null; for (int l = 0; l < c.getAttributes().getLength(); l++) { Node a = c.getAttributes().item(l); if (a.getNodeName().equals("name") && a.getNodeValue().equals(mCurrency)) { if (DEBUG > 1) Log.d(TAG, " -- Got the Currency!!!"); bool = true; } else if (a.getNodeName().equals("rate")) { if (DEBUG > 1) Log.d(TAG, " -- Got the RATE!!!" + a.getNodeValue()); rateTmp = a.getNodeValue(); } if (bool && rateTmp != null) { rateStr = rateTmp; if (DEBUG > 1) Log.d(TAG, " -- Got the Currency VALUE: " + rateStr); // stop the loop break; } } } // stop the loop if (rateStr != null) { break; } } } } // stop the loop if (rateStr != null) { break; } } } // stop the loop if (rateStr != null) { break; } } if (DEBUG > 1) Log.d(TAG, " - Parse end"); } } catch (Exception e) { e.printStackTrace(); return 1; } // Check the values if (dateStr == null || rateStr == null) { Log.d(TAG, " # One of the values is null!"); return 1; } setCurrencyDate(dateStr); setCurrencyRate(Float.parseFloat(rateStr)); return 0; }
From source file:com.datatorrent.stram.client.DTConfiguration.java
public void loadFile(File file, Scope defaultScope) throws IOException, ParserConfigurationException, SAXException, ConfigException { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file); Element documentElement = doc.getDocumentElement(); if (!documentElement.getNodeName().equals("configuration")) { throw new ConfigException("Root element needs to be \"configuration\""); }//from w w w . j a va2s. c om if (doc.hasChildNodes()) { NodeList propertyNodes = documentElement.getChildNodes(); for (int i = 0; i < propertyNodes.getLength(); i++) { Node propertyNode = propertyNodes.item(i); if (propertyNode.getNodeType() == Node.ELEMENT_NODE) { if (propertyNode.getNodeName().equals("property")) { processPropertyNode((Element) propertyNode, defaultScope); } else { LOG.warn("Ignoring unknown element {}", propertyNode.getNodeName()); } } } } }
From source file:it.polito.tellmefirst.web.rest.clients.ClientEpub.java
private HashMap<String, String> parseEpub(File file) throws IOException, TMFVisibleException { LOG.debug("[parseEpub] - BEGIN"); ZipFile fi = new ZipFile(file); for (Enumeration e = fi.entries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); if (entry.getName().endsWith("ncx")) { InputStream tocMaybeDirty = fi.getInputStream(entry); Scanner scanner = new Scanner(tocMaybeDirty, "UTF-8").useDelimiter("\\A"); String theString = scanner.hasNext() ? scanner.next() : ""; tocMaybeDirty.close();//from w w w . ja v a2s . c o m scanner.close(); String res = theString.replaceAll(">[\\s]*?<", "><"); InputStream toc = new ByteArrayInputStream(res.getBytes(StandardCharsets.UTF_8)); try { DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder.parse(toc); toc.close(); if (doc.hasChildNodes()) { findNavMap(doc.getChildNodes()); } } catch (Exception ex) { LOG.error("Unable to navigate the TOC"); } removeEmptyTOC(epub); //search anchors in links and split Set set = epub.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); if (me.getValue().toString().contains("#")) { String[] parts = me.getValue().toString().split("#"); String anchor = parts[1]; epub.put(me.getKey().toString(), anchor); } } } if (entry.getName().endsWith("opf")) { //manage files because order is important InputStream content = fi.getInputStream(entry); Scanner scanner = new Scanner(content, "UTF-8").useDelimiter("\\A"); String contentString = scanner.hasNext() ? scanner.next() : ""; content.close(); scanner.close(); String filenameRegex = "href=\"(.*.htm(|l))\".*media-type=\"application/xhtml"; Pattern pattern = Pattern.compile(filenameRegex); Matcher matcher = pattern.matcher(contentString); Integer count = 0; while (matcher.find()) { files.put(count, matcher.group(1)); count++; } } if (entry.getName().endsWith("html") || entry.getName().endsWith("htm") || entry.getName().endsWith("xhtml")) { InputStream htmlFile = fi.getInputStream(entry); Scanner scanner = new Scanner(htmlFile, "UTF-8").useDelimiter("\\A"); String htmlString = scanner.hasNext() ? scanner.next() : ""; String regex1 = htmlString.replaceAll("^[^_]*?<body>", ""); //remove head String regex2 = regex1.replaceAll("</body>.*$", ""); //remove tail String htmlCleaned = regex2.replaceAll("<a.*?/>", ""); //anchor with one tag String[] bits = entry.getName().split("/"); String fileName = bits[bits.length - 1]; htmls.put(fileName, htmlCleaned); } } fi.close(); Integer i; for (i = 0; i < files.size(); i++) { stringBuilder.append("<p id=\"" + files.get(i) + "\"></p>"); // "anchor" also the heads of each files stringBuilder.append(htmls.get(files.get(i))); } String htmlAll = stringBuilder.toString(); /* We have all needed files, start to split For each link -> made a chunk Start from the bottom */ Metadata metadata = new Metadata(); Parser parser = new HtmlParser(); ListIterator<Map.Entry<String, String>> iter = new ArrayList<>(epub.entrySet()).listIterator(epub.size()); while (iter.hasPrevious()) { Map.Entry<String, String> me = iter.previous(); try { ContentHandler contenthandler = new BodyContentHandler(10 * htmlAll.length()); Scanner sc = new Scanner(htmlAll); sc.useDelimiter("id=\"" + me.getValue().toString() + "\">"); htmlAll = sc.next(); InputStream stream = new ByteArrayInputStream(sc.next().getBytes(StandardCharsets.UTF_8)); parser.parse(stream, contenthandler, metadata, new ParseContext()); String chapterText = contenthandler.toString().toLowerCase().replaceAll("\\d+.*", ""); String chapterTextWithoutNo = chapterText.replaceAll("\\d+.*", ""); // Remove the Project Gutenberg meta information from the text String chapterTextCleaned = chapterTextWithoutNo.split("end of the project gutenberg ebook")[0]; epub.put(me.getKey().toString(), chapterTextCleaned); } catch (Exception ex) { LOG.error("Unable to parse content for index: " + me.getKey() + ", this chapter will be deleted"); removeChapter(epub, me.getKey().toString()); } } /* I remove the Project Gutenberg license chapter from the Map, because it is useless for the classification and it generates a Lucene Exception in case of the Italian language (the license text is always in English). You can use this method in order to remove each chapter that is useless for classifying your Epub document. */ removeChapter(epub, "A Word from Project Gutenberg"); removeEmptyItems(epub); //If the Epub file has a bad structure, I try to use the basic Epub extractor of Tika. if (epub.size() == 0) { LOG.info("The Epub file has a bad structure. Try to use the Tika extractor"); epub.put("All text", autoParseAll(file)); } removeEmptyItems(epub); if (epub.size() == 0) { LOG.error("Unable to extract text from this Epub"); throw new TMFVisibleException("Unable to extract any text from this Epub."); } removeDownloadedFile(TEMPORARY_PATH); LOG.debug("[parseEpub] - END"); return epub; }
From source file:com.github.podd.resources.RestletPoddClientImpl.java
public Representation doSPARQL2(final String queryString, final Collection<InferredOWLOntologyID> artifactIds) throws PoddException { this.log.debug("cookies: {}", this.currentCookies); final ClientResource resource = new ClientResource(this.getUrl(PoddWebConstants.PATH_SPARQL)); resource.getCookies().addAll(this.currentCookies); final Form form = new Form(); form.add(PoddWebConstants.KEY_SPARQLQUERY, queryString); // TODO: Parse query to make sure it is syntactically valid before sending query resource.addQueryParameter(PoddWebConstants.KEY_SPARQLQUERY, queryString); try {// ww w.j a va 2 s . c om final Representation get = resource.get(MediaType.APPLICATION_ALL_XML); /* try { String d = get.getText().replaceAll("(.*)version=(.*)", ""); String f = d.replaceAll("(.*)sparql(.*)", "").replaceAll("(.*)result(.*)", ""); System.out.println(f); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ try { DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputStream in = IOUtils.toInputStream(get.getText()); Document doc = dBuilder.parse(in); System.out.println("\r\n" + "============================== \r\n" + " PODD Query Results \r\n" + "=============================="); if (doc.hasChildNodes()) { printNote(doc.getChildNodes()); } } catch (Exception e) { System.out.println(e.getMessage()); } return get; } catch (final ResourceException e) { if (e.getStatus().equals(Status.CLIENT_ERROR_PRECONDITION_FAILED)) { System.out.println(""); System.out.println("Error: Access denied in server " + this.serverUrl + " with the login credentials provided in ~/poddclient.properties."); // Precondition failed indicates that they do not have access to any artifacts, so // return empty results set return null; } System.out.println(e.toString()); } catch (final UnsupportedRDFormatException e) { // Attempt to retry the request once to avoid random restlet failures stopping the // entire process try { final Representation get = resource.post(form.getWebRepresentation(CharacterSet.UTF_8), RestletUtilMediaType.APPLICATION_RDF_JSON); // Pass the desired format to the get method of the ClientResource // final Representation get = // resource.get(RestletUtilMediaType.APPLICATION_RDF_JSON); final StringWriter writer = new StringWriter(4096); get.write(writer); return null; } catch (final ResourceException e1) { if (e1.getStatus().equals(Status.CLIENT_ERROR_PRECONDITION_FAILED)) { System.out.println(""); System.out.println("Error: Access denied in server " + this.serverUrl + " with login credentials provided in ~/poddclient.properties."); // Precondition failed indicates that they do not have access to any artifacts, // so // return empty results set return null; } else { System.out.println(e.toString()); } } catch (final IOException | UnsupportedRDFormatException e1) { System.out.println(e.toString()); } } return null; }
From source file:com.github.podd.resources.RestletPoddClientImpl.java
public Representation doSPARQL3(final String queryString, final Collection<InferredOWLOntologyID> artifactIds) throws PoddException { this.log.debug("cookies: {}", this.currentCookies); //final Context context = new Context(); //context.getParameters().add("socketTimeout", "10000"); //context.getParameters().add("connectionTimeout", "10000"); final ClientResource resource = new ClientResource(this.getUrl(PATH_SPARQL2)); resource.getCookies().addAll(this.currentCookies); final Form form = new Form(); form.add(PoddWebConstants.KEY_SPARQLQUERY, queryString); // TODO: Parse query to make sure it is syntactically valid before sending query resource.addQueryParameter(PoddWebConstants.KEY_SPARQLQUERY, queryString); try {/*from w w w . ja va2 s .c o m*/ final Representation get = resource.get(MediaType.APPLICATION_ALL_XML); /* try { String d = get.getText().replaceAll("(.*)version=(.*)", ""); String f = d.replaceAll("(.*)sparql(.*)", "").replaceAll("(.*)result(.*)", ""); System.out.println(f); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ try { DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); String d = get.getText().replaceAll("(.*)version=(.*)", ""); String f = d.replaceAll("(.*)sparql(.*)", ""); InputStream in = IOUtils .toInputStream("<?xml version='1.0' encoding='UTF-8'?>" + " <r>" + f + " </r>", "UTF-8"); Document doc = dBuilder.parse(in); System.out.println("\r\n" + "============================== \r\n" + " PODD Query Results \r\n" + "=============================="); if (doc.hasChildNodes()) { printNote3(doc.getChildNodes()); } } catch (Exception e) { System.out.println(e.getMessage()); } return get; } catch (final ResourceException e) { if (e.getStatus().equals(Status.CLIENT_ERROR_PRECONDITION_FAILED)) { System.out.println(""); System.out.println("Error: Access denied in server " + this.serverUrl + " with the login credentials provided in ~/poddclient.properties."); // Precondition failed indicates that they do not have access to any artifacts, so // return empty results set return null; } System.out.println(e.toString()); } catch (final UnsupportedRDFormatException e) { // Attempt to retry the request once to avoid random restlet failures stopping the // entire process try { final Representation get = resource.post(form.getWebRepresentation(CharacterSet.UTF_8), RestletUtilMediaType.APPLICATION_RDF_JSON); // Pass the desired format to the get method of the ClientResource // final Representation get = // resource.get(RestletUtilMediaType.APPLICATION_RDF_JSON); final StringWriter writer = new StringWriter(4096); get.write(writer); return null; } catch (final ResourceException e1) { if (e1.getStatus().equals(Status.CLIENT_ERROR_PRECONDITION_FAILED)) { System.out.println(""); System.out.println("Error: Access denied in server " + this.serverUrl + " with login credentials provided in ~/poddclient.properties."); // Precondition failed indicates that they do not have access to any artifacts, // so // return empty results set return null; } else { System.out.println("Error: Unable to find the requested data in PODD"); System.out.println(e.toString()); } } catch (final IOException | UnsupportedRDFormatException e1) { System.out.println("Error: Unable to find the requested data in PODD"); System.out.println(e.toString()); return null; } } // Your database code here return null; }
From source file:org.etudes.ambrosia.impl.UiServiceImpl.java
/** * {@inheritDoc}/* ww w . j a v a 2s. c o m*/ */ public Fragment newFragment(InputStream in) { UiFragment frag = null; Document doc = Xml.readDocumentFromStream(in); if ((doc == null) || (!doc.hasChildNodes())) return frag; // allowing for comments, use the first element node that is our fragment NodeList nodes = doc.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; if (!(((Element) node).getTagName().equals("fragment"))) continue; // build the fragment from this element frag = new UiFragment(this, (Element) node); break; } // else we have an invalid if (frag == null) { M_log.warn("newFragment: element \"fragment\" not found in stream xml"); frag = new UiFragment(); } return frag; }