List of usage examples for org.w3c.dom Document getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
/** * Parses a XML document./*www. j a v a 2 s. c o m*/ * * @param xml the XML document * @return DOM */ public static Document parse(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xml))); removeWhitespaces((Element) document.getChildNodes().item(0)); return document; }
From source file:Main.java
public static void ReadXMLFile2() { try {/* w w w . j a va2 s. c o m*/ 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:uk.ac.kcl.Transform23.java
public static String convertHL7ToJson(Message message) { try {// www .j ava 2s .c o m DefaultXMLParser xmlParser = new DefaultXMLParser(new CanonicalModelClassFactory("2.4")); Document xml = xmlParser.encodeDocument(message); cleanFieldNames(xml.getChildNodes().item(0)); XmlMapper xmlMapper = new XmlMapper(); List entries = null; try { entries = xmlMapper.readValue(getStringFromDocument(xml), List.class); } catch (IOException | TransformerException ex) { Logger.getLogger(Transform23.class.getName()).log(Level.SEVERE, null, ex); } ObjectMapper jsonMapper = new ObjectMapper(); String json = null; try { json = jsonMapper.writeValueAsString(entries); } catch (IOException ex) { Logger.getLogger(Transform23.class.getName()).log(Level.SEVERE, null, ex); } //System.out.println(json); json = json.substring(1, (json.length() - 1)); //to do - add code to rename fields, removing periods return json; } catch (HL7Exception ex) { Logger.getLogger(Transform23.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:uk.ac.kcl.Transform231.java
public static String convertHL7ToJson(Message message) { try {/* w ww .ja va2 s . c o m*/ DefaultXMLParser xmlParser = new DefaultXMLParser(new CanonicalModelClassFactory("2.4")); Document xml = xmlParser.encodeDocument(message); cleanFieldNames(xml.getChildNodes().item(0)); XmlMapper xmlMapper = new XmlMapper(); List entries = null; try { entries = xmlMapper.readValue(getStringFromDocument(xml), List.class); } catch (IOException | TransformerException ex) { Logger.getLogger(Transform231.class.getName()).log(Level.SEVERE, null, ex); } ObjectMapper jsonMapper = new ObjectMapper(); String json = null; try { json = jsonMapper.writeValueAsString(entries); } catch (IOException ex) { Logger.getLogger(Transform231.class.getName()).log(Level.SEVERE, null, ex); } //System.out.println(json); json = json.substring(1, (json.length() - 1)); //to do - add code to rename fields, removing periods return json; } catch (HL7Exception ex) { Logger.getLogger(Transform231.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:Main.java
public static String[] getAllTokens(Document doc, boolean hasCorpusElement) { ArrayList<String> toReturnAL = new ArrayList<String>(); if (hasCorpusElement) { NodeList corpusDocs = doc.getChildNodes().item(0).getChildNodes(); for (int d = 0; d < corpusDocs.getLength(); d++) { if (!corpusDocs.item(d).getNodeName().equals("doc")) continue; //System.out.println(doc.getChildNodes().getLength()); NodeList sentences = corpusDocs.item(d).getChildNodes(); for (int i = 0; i < sentences.getLength(); i++) { if (!sentences.item(i).getNodeName().equals("s")) continue; NodeList tokens = sentences.item(i).getChildNodes(); for (int j = 0; j < tokens.getLength(); j++) { Node tokenNode = tokens.item(j); if (tokenNode.getNodeName().equals("toponym")) { toReturnAL.add(tokenNode.getAttributes().getNamedItem("term").getNodeValue()); } else if (tokenNode.getNodeName().equals("w")) { toReturnAL.add(tokenNode.getAttributes().getNamedItem("tok").getNodeValue()); }//ww w . j ava 2s .c o m } } } } else { NodeList sentences = doc.getChildNodes().item(1).getChildNodes(); for (int i = 0; i < sentences.getLength(); i++) { if (!sentences.item(i).getNodeName().equals("s")) continue; NodeList tokens = sentences.item(i).getChildNodes(); for (int j = 0; j < tokens.getLength(); j++) { Node tokenNode = tokens.item(j); if (tokenNode.getNodeName().equals("toponym")) { toReturnAL.add(tokenNode.getAttributes().getNamedItem("term").getNodeValue()); } else if (tokenNode.getNodeName().equals("w")) { toReturnAL.add(tokenNode.getAttributes().getNamedItem("tok").getNodeValue()); } } } } return toReturnAL.toArray(new String[0]); }
From source file:net.bpelunit.test.util.TestUtil.java
public static Element parse(String toEncode) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*ww w . j a v a2s. c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader("<dummy>" + toEncode + "</dummy>"))); // should be one element Element dummy = (Element) document.getChildNodes().item(0); return dummy; }
From source file:de.matzefratze123.heavyspleef.util.I18NNew.java
private static void loadMessages() { InputStream inStream = null;/*from www. ja va 2 s .c om*/ try { File file = new File(HeavySpleef.getInstance().getDataFolder(), MESSAGES_FILE); if (file.exists()) { inStream = new FileInputStream(file); } else { inStream = I18NNew.class.getResourceAsStream('/' + MESSAGES_FILE); } if (inStream == null) { throw new IOException("null"); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document xml = builder.parse(inStream); NodeList nodes = xml.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeName().equalsIgnoreCase(MESSAGES_TAG)) { //Messages entry! readEntry(node, ""); } } } catch (Exception e) { Logger.severe("Failed to load language messages: " + e.getMessage() + ". EXPECT ERRORS!"); e.printStackTrace(); } finally { try { if (inStream != null) { inStream.close(); } } catch (Exception e) { } } }
From source file:de.matzefratze123.staffinformer.util.I18N.java
public static void loadMessages() { messages.clear();/*w ww . j a v a 2 s . com*/ InputStream inStream = null; try { File file = new File(StaffInformer.getInstance().getDataFolder(), MESSAGES_FILE); if (file.exists()) { inStream = new FileInputStream(file); } else { inStream = I18N.class.getResourceAsStream('/' + MESSAGES_FILE); } if (inStream == null) { throw new IOException("null"); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document xml = builder.parse(inStream); NodeList nodes = xml.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeName().equalsIgnoreCase(MESSAGES_TAG)) { //Messages entry! readEntry(node, ""); } } } catch (Exception e) { Logger.severe("Failed to load language messages: " + e.getMessage() + ". EXPECT ERRORS!"); e.printStackTrace(); } finally { try { if (inStream != null) { inStream.close(); } } catch (Exception e) { } } }
From source file:Main.java
public static List<Map<String, String>> ReadFolderItemsFromFile(String path, String filename, String folderID) { List<Map<String, String>> results = new ArrayList<Map<String, String>>(); try {//from w w w .j a va2 s . c o m File fXmlFile = new File(path, filename); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = null; if (folderID == null) { // get all the children of the root nList = doc.getChildNodes().item(0).getChildNodes(); } else { //Log.d("ReadItemsFromFile", "Current FolderID: " + folderID); NodeList folderList = doc.getElementsByTagName("folder"); for (int i = 0; i < folderList.getLength(); i++) { Node curNode = folderList.item(i); if (curNode.getNodeType() == Node.ELEMENT_NODE) { String curNodeId = ((Element) curNode).getElementsByTagName("id").item(0).getTextContent(); //Log.d("ReadItemsFromFile", "Number of items: " + curNodeId); if (curNodeId.equals(folderID)) { //Log.d("ReadItemsFromFile", "Found the folder"); NodeList folderChildren = curNode.getChildNodes(); for (int j = 0; j < folderChildren.getLength(); j++) { //Log.d("ReadItemsFromFile", "node name: " + folderChildren.item(j).getNodeName()); if (folderChildren.item(j).getNodeName().equals("contents")) { //Log.d("ReadItemsFromFile", "found the contents child"); nList = folderChildren.item(j).getChildNodes(); break; } } break; } } } } if (nList != null) { Log.d("ReadItemsFromFile", "-----------------------"); Log.d("ReadItemsFromFile", "Number of items: " + nList.getLength()); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); //Log.d("ReadItemsFromFile", temp + ". node type: " + nNode.getNodeType()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; Node elementNameNode = eElement.getElementsByTagName("name").item(0); String elementNameString = elementNameNode.getTextContent(); //Log.d("ReadItemsFromFile", "Name: " + elementNameString); HashMap<String, String> mapElement = new HashMap<String, String>(); mapElement.put("name", elementNameString); mapElement.put("id", eElement.getElementsByTagName("id").item(0).getTextContent()); mapElement.put("type", nNode.getNodeName()); results.add(mapElement); } } } } catch (Exception e) { e.printStackTrace(); return null; } return results; }
From source file:kenh.xscript.ScriptUtils.java
/** * Get xScript instance./*from w w w. j a v a2 s .co m*/ * @param doc * @param env * @return */ public static final Element getInstance(Document doc, Environment env) throws UnsupportedScriptException { NodeList children = doc.getChildNodes(); Node node = null; for (int i = 0; i < children.getLength(); i++) { Node n = children.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { node = n; break; } } return getInstance(node, env); }