List of usage examples for org.w3c.dom Node hasChildNodes
public boolean hasChildNodes();
From source file:com.grameenfoundation.ictc.controllers.TestServlet.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w w w. java 2 s. c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SAXException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet TestServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); //String theString = IOUtils.toString(request.getInputStream(), "UTF-8"); //out.println("<h1>Servlet TestServlet at " +theString + "</h1>"); //System.out.println(theString); File xmlFile = new File("/home/grameen/test.xml"); DocumentBuilderFactory db = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = db.newDocumentBuilder(); } catch (ParserConfigurationException ex) { Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex); } Document doc = builder.parse(xmlFile); doc.getDocumentElement().normalize(); ; System.out.println("Root element :" + doc.getDocumentElement()); Node node = doc.getDocumentElement(); System.out.println(node.hasChildNodes()); String nodeName = null; org.neo4j.graphdb.Node nid = null; NodeList n = doc.getElementsByTagName("sObject"); NodeList nlst = node.getChildNodes(); //Node object = n.item(1); System.out.println("object " + node.getChildNodes().getLength()); // for (int i = 1; i < n.getLength(); i++) { // // Node nd = n.item(i); // // nodeName = nd.getNodeName().substring(3); // System.out.println(nodeName); // String nnn = nodeName.substring(0,nodeName.indexOf('_') ); // nid.setProperty(nnn, nd.getNodeValue()); // // System.out.println(nnn); // // } // // BiodataWrapper b = new BiodataWrapper(); // // b.setFirstName("Kwesi "); // b.setLastName("Asera"); // b.setCommunity("Akumadan"); // b.setDistrict("Akumadan"); // b.setVillage("Akumadan"); // b.setRegion("Brong Ahafo Region"); // b.setGender("Male"); // b.setMaritalStatus("Divorced"); // b.setNickname("Ratata"); // b.setNumberOfChildren("2"); // b.setNumberOfDependants("3"); // b.setAge("45"); // b.setDistrict("Akumadan"); // b.setEducation("Secondary School"); // // // new BiodataModel().createBiodata(b); // Biodata bi = new BiodataModel().getBiodata(Biodata.LAST_NAME,"Asera"); // // FarmManagementWrapper fmw = new FarmManagementWrapper(); // fmw.setFamilyLabor("2"); // fmw.setFboMember("yes"); // fmw.setFboName("Cool FBO"); // fmw.setReferenceLeadFarmer("Kwame Abakah"); // fmw.setBankAccount("yes"); // fmw.setFarmRecordKeeping("yes"); // fmw.setProductionObjective("2. Sells about 50% of produce."); // fmw.setDispostionToRisk("Annually"); // fmw.setEntrepreneurship("1.At least every other year"); // fmw.setLabourUse("1.My family manage the farm, no contribution to manual labor."); // // // // // FarmManagement fm = new FarmManagementModel().createFM(fmw); // HarvestWrapper hw = new HarvestWrapper(); // hw.setTimeCropReadyToHarvest("2.Middle of July"); // hw.setTimeCropHarvested("February"); // hw.setNoLabourtotal("5"); // hw.setNoFamilyLabour("2"); // hw.setNoHiredLabour("3"); // hw.setCostOfHiredLabour("150"); // hw.setTimeCompletionHarvest("4"); // // // Harvest hv = new HarvestModel().createFM(hw); // PostHarvestWrapper p = new PostHarvestWrapper(); // PostHarvest ph = new PostHarvestModel().createPostHarvest(p); // StorageWrapper s = new StorageWrapper(); // Storage storage = new StorageModel().createFM(s); //MarketingWrapper m = new MarketingWrapper(); //Marketing mk = new MarketingModel().createMarketing(m); // TechnicalNeedsWrapper tech = new TechnicalNeedsWrapper(); // TechnicalNeed t = new TechnicalNeedsModel().createTechNeeds(tech); // // // BiodataModel bio = new BiodataModel(); // // //new BiodataModel().BiodataToHarvest("Asera",hv.getUnderlyingNode()); // // //bio.BiodataToPostHarvest("Asera",ph.getUnderlyingNode()); // //bio.BiodataToStorage("Asera",storage.getUnderlyingNode()); // // bio.BiodataToMarketing("Asera",mk.getUnderlyingNode()); // bio.BiodataToTechNeeds("Asera",t.getUnderlyingNode()); // String doc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + //" <soapenv:Body>\n" + //" <notifications xmlns=\"http://soap.sforce.com/2005/09/outbound\">\n" + //" <OrganizationId>00D25000000D1WMEA0</OrganizationId>\n" + //" <ActionId>04k25000000003FAAQ</ActionId>\n" + //" <SessionId>00D25000000D1WM!AQUAQJPhwqlIm5elOzluRMpKPTbpgb5enecFP2aawxYeqv.tHHXp6ooKOolfTsG76QMFBB7xY_LMjygKjw3.EAEu2liGA.ys</SessionId>\n" + //" <EnterpriseUrl>https://taroworks-6058--sandbox.cs80.cloudforce.com/services/Soap/c/34.0/00D25000000D1WM</EnterpriseUrl>\n" + //" <PartnerUrl>https://taroworks-6058--sandbox.cs80.cloudforce.com/services/Soap/u/34.0/00D25000000D1WM</PartnerUrl>\n" + //" <Notification>\n" + //" <Id>04l250000000Z3UAAU</Id>\n" + //" <sObject xsi:type=\"sf:Cluster1__c\" xmlns:sf=\"urn:sobject.enterprise.soap.sforce.com\">\n" + //" <sf:Id>a1G25000000A2j1EAC</sf:Id>\n" + //" <sf:Farmer_id__c>234556</sf:Farmer_id__c>\n" + //" <sf:farmer_name__c>Kuuku</sf:farmer_name__c>\n" + //" </sObject>\n" + //" </Notification>\n" + //" </notifications>\n" + //" </soapenv:Body>\n" + //"</soapenv:Envelope>" ; // // Document docu = XMLParser.parseXmlText(doc); // // System.out.println("document" + docu.toString()); } }
From source file:org.liberty.android.fantastischmemopro.downloader.DownloaderFE.java
private void downloadDatabase(DownloadItem di) throws Exception { HttpClient httpclient = new DefaultHttpClient(); String url = di.getAddress(); Log.i(TAG, "Url: " + url); HttpGet httpget = new HttpGet(url); HttpResponse response;/*from w w w . j a v a2 s . com*/ response = httpclient.execute(httpget); Log.i(TAG, "Response: " + response.getStatusLine().toString()); HttpEntity entity = response.getEntity(); if (entity == null) { throw new Exception("Null entity error"); } InputStream instream = entity.getContent(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); Document doc = documentBuilder.parse(instream); NodeList nodes = doc.getElementsByTagName("cardSet"); nodes = doc.getElementsByTagName("error"); if (nodes.getLength() > 0) { throw new Exception("Invalid ID to retrieve the database!"); } List<Item> itemList = new LinkedList<Item>(); nodes = doc.getElementsByTagName("flashcard"); int nodeNumber = nodes.getLength(); for (int i = 0; i < nodeNumber; i++) { Node node = nodes.item(i); if (!node.hasChildNodes()) { continue; } NodeList childNodes = node.getChildNodes(); int childNodeNumber = childNodes.getLength(); Item item = new Item(); item.setId(i + 1); for (int j = 0; j < childNodeNumber; j++) { Node childNode = childNodes.item(j); if (childNode.hasChildNodes()) { if (childNode.getNodeName().equals("question")) { item.setQuestion(childNode.getFirstChild().getNodeValue()); } else if (childNode.getNodeName().equals("answer")) { item.setAnswer(childNode.getFirstChild().getNodeValue()); } } } if (!item.getQuestion().equals("")) { itemList.add(item); } } instream.close(); /* Now create the databased from the itemList */ String dbname = di.getTitle() + ".db"; /* Replace illegal characters */ dbname = dbname.replaceAll("[`~!#@%&*{};:'\"]", "_"); String dbpath = Environment.getExternalStorageDirectory().getAbsolutePath() + getString(R.string.default_dir); DatabaseHelper.createEmptyDatabase(dbpath, dbname); DatabaseHelper dbHelper = new DatabaseHelper(this, dbpath, dbname); dbHelper.insertListItems(itemList); dbHelper.close(); }
From source file:elaborate.editor.export.tei.TeiMaker.java
private int processEntry(int _pageno, Element body, String _currentFolio, ProjectEntry projectEntry) { int pageno = _pageno; String currentFolio = _currentFolio; String folio = StringUtils.defaultIfBlank(projectEntry.getMetadataValue("Folio number"), "") + StringUtils.defaultIfBlank(projectEntry.getMetadataValue("Folio side"), ""); if (!currentFolio.equals(folio)) { pageno = addPb(body, pageno, projectEntry, folio); }//from w ww. j a v a 2 s .c om // addCb(body, projectEntry); currentFolio = folio; Element entryDiv = tei.createElement("div"); entryDiv.setAttribute("xml:id", "e" + projectEntry.getId()); entryDiv.setAttribute("n", projectEntry.getName()); addEntryInterpGrp(entryDiv, projectEntry); List<Transcription> orderedTranscriptions = Lists.newArrayList(projectEntry.getTranscriptions()); Collections.sort(orderedTranscriptions, ORDER_BY_TYPE); for (Transcription transcription : orderedTranscriptions) { HtmlTeiConverter htmlTeiConverter = new HtmlTeiConverter(transcription.getBody(), config, transcription.getTranscriptionType().getName(), entityManager); Node transcriptionNode = htmlTeiConverter.getContent(); Node importedTranscriptionNode = tei.importNode(transcriptionNode, true); Node child = importedTranscriptionNode.getFirstChild(); while (child != null) { Node nextSibling = child.getNextSibling(); if (child.getNodeName().equals("div") && child.hasChildNodes()) { entryDiv.appendChild(child); } child = nextSibling; } } body.appendChild(entryDiv); return pageno; }
From source file:org.dasein.cloud.cloudstack.CSMethod.java
private ParsedError parseError(int httpStatus, String assumedXml) throws InternalException { Logger logger = CSCloud.getLogger(CSMethod.class, "std"); if (logger.isTraceEnabled()) { logger.trace(/*www . j a v a2 s. co m*/ "enter - " + CSMethod.class.getName() + ".parseError(" + httpStatus + "," + assumedXml + ")"); } try { ParsedError error = new ParsedError(); error.code = httpStatus; error.message = null; try { Document doc = parseResponse(httpStatus, assumedXml); NodeList codes = doc.getElementsByTagName("errorcode"); for (int i = 0; i < codes.getLength(); i++) { Node n = codes.item(i); if (n != null && n.hasChildNodes()) { error.code = Integer.parseInt(n.getFirstChild().getNodeValue().trim()); } } NodeList text = doc.getElementsByTagName("errortext"); for (int i = 0; i < text.getLength(); i++) { Node n = text.item(i); if (n != null && n.hasChildNodes()) { error.message = n.getFirstChild().getNodeValue(); } } } catch (Throwable ignore) { logger.warn("parseError(): Error was unparsable: " + ignore.getMessage()); if (error.message == null) { error.message = assumedXml; } } if (error.message == null) { if (httpStatus == 401) { error.message = "Unauthorized user"; } else if (httpStatus == 430) { error.message = "Malformed parameters"; } else if (httpStatus == 547 || httpStatus == 530) { error.message = "Server error in cloud (" + httpStatus + ")"; } else if (httpStatus == 531) { error.message = "Unable to find account"; } else { error.message = "Received error code from server: " + httpStatus; } } return error; } finally { if (logger.isTraceEnabled()) { logger.trace("exit - " + CSMethod.class.getName() + ".parseError()"); } } }
From source file:de.ingrid.portal.scheduler.jobs.UpgradeClientJob.java
public Node findSubNode(String name, Node node) { if (node.getNodeType() != Node.ELEMENT_NODE) { log.error("Error: Search node not of element type"); return null; }/*from www .j av a 2 s . co m*/ if (!node.hasChildNodes()) return null; NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node subnode = list.item(i); if (subnode.getNodeType() == Node.ELEMENT_NODE) { if (subnode.getNodeName().equals(name)) return subnode; } } return null; }
From source file:com.nortal.jroad.endpoint.AbstractXTeeBaseEndpoint.java
private void copyParing(Document paring, Node response) throws Exception { Node paringElement = response.appendChild(response.getOwnerDocument().createElement("paring")); Node kehaNode = response.getOwnerDocument().importNode(paring.getDocumentElement(), true); NamedNodeMap attrs = kehaNode.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { paringElement.getAttributes().setNamedItem(attrs.item(i).cloneNode(true)); }//from w w w . j a v a2 s.c o m while (kehaNode.hasChildNodes()) { paringElement.appendChild(kehaNode.getFirstChild()); } }
From source file:com.signavio.warehouse.business.util.jpdl4.Script.java
public Script(org.w3c.dom.Node script) { this.uuid = "oryx_" + UUID.randomUUID().toString(); NamedNodeMap attributes = script.getAttributes(); this.name = JpdlToJson.getAttribute(attributes, "name"); this.expression = JpdlToJson.getAttribute(attributes, "expr"); this.language = JpdlToJson.getAttribute(attributes, "lang"); this.variable = JpdlToJson.getAttribute(attributes, "var"); if (script.hasChildNodes()) for (org.w3c.dom.Node a = script.getFirstChild(); a != null; a = a.getNextSibling()) if (a.getNodeName().equals("text")) { this.text = a.getTextContent(); break; }//from w ww . j a v a 2 s . co m this.bounds = JpdlToJson.getBounds(attributes.getNamedItem("g")); }
From source file:com.rubika.aotalk.util.ItemRef.java
private final String getElementValue(Node elem) { Node child;//from w w w.jav a2 s .co m if (elem != null) { if (elem.hasChildNodes()) { for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.TEXT_NODE) { return child.getNodeValue(); } } } } return ""; }
From source file:com.signavio.warehouse.business.util.jpdl4.Sql.java
public Sql(org.w3c.dom.Node sql) { this.uuid = "oryx_" + UUID.randomUUID().toString(); NamedNodeMap attributes = sql.getAttributes(); this.name = JpdlToJson.getAttribute(attributes, "name"); this.unique = Boolean.parseBoolean(JpdlToJson.getAttribute(attributes, "unique")); this.var = JpdlToJson.getAttribute(attributes, "var"); this.bounds = JpdlToJson.getBounds(attributes.getNamedItem("g")); if (sql.hasChildNodes()) for (org.w3c.dom.Node a = sql.getFirstChild(); a != null; a = a.getNextSibling()) { if (a.getNodeName().equals("query")) this.query = a.getTextContent(); if (a.getNodeName().equals("parameters")) this.parameters = new Parameters(a); }//www . j av a 2 s . c o m }
From source file:Main.java
protected static void print(PrintStream out, Node node) { if (node == null) return;//from w ww . ja v a 2 s. c om short type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: { out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); NodeList nodelist = node.getChildNodes(); int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType docType = (DocumentType) node; out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n"); break; } case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); NamedNodeMap map = node.getAttributes(); if (map != null) { int size = map.getLength(); for (int i = 0; i < size; i++) { Attr attr = (Attr) map.item(i); out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } } if (!node.hasChildNodes()) out.print("/>"); else { out.print('>'); NodeList nodelist = node.getChildNodes(); int numChildren = nodelist.getLength(); for (int i = 0; i < numChildren; i++) print(out, nodelist.item(i)); out.print("</"); out.print(node.getNodeName()); out.print('>'); } break; } case Node.ENTITY_REFERENCE_NODE: { NodeList nodelist = node.getChildNodes(); if (nodelist != null) { int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); } break; } case Node.CDATA_SECTION_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String s = node.getNodeValue(); if (s != null && s.length() > 0) { out.print(' '); out.print(s); } out.print("?>"); break; } case Node.COMMENT_NODE: { out.print("<!--"); out.print(node.getNodeValue()); out.print("-->"); break; } default: { out.print(normalize(node.getNodeValue())); break; } } out.flush(); }