List of usage examples for org.w3c.dom Document getFirstChild
public Node getFirstChild();
From source file:Main.java
public static Node getRoot(String documentAsString) { final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try {// w ww . j av a 2 s. co m final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); final Document document = documentBuilder.parse(new InputSource(new StringReader(documentAsString))); return document.getFirstChild(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } catch (SAXException e) { throw new IllegalArgumentException(e); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:Main.java
public static Node parseXML(String text) throws SAXException, ParserConfigurationException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = dbf.newDocumentBuilder(); InputSource source = new InputSource(new StringReader(text)); Document doc = parser.parse(source); if (doc == null) { throw new NullPointerException(); }//www . j av a 2 s . c om return doc.getFirstChild(); }
From source file:Main.java
public static Map<String, String> SimpleDocumentToMap(Document doc) { Map<String, String> simpleMap = new HashMap<String, String>(); //trim off outter layer Node node = doc.getFirstChild(); NodeList nList = node.getChildNodes(); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (eElement != null) { simpleMap.put(eElement.getTagName(), eElement.getTextContent()); } // end if eelement } // end if nnode.getnodetype() } //end for int temp return simpleMap; }
From source file:Main.java
/** * Creates (only if necessary) and returns the element which is at the end of the specified * path.// ww w . ja v a 2 s . c o m * @param doc the target document where the specified path should be created * @param path a dot separated string indicating the path to be created * @return the component at the end of the newly created path. */ public static Element createLastPathComponent(Document doc, String[] path) { Element parent = (Element) doc.getFirstChild(); if (path == null || parent == null || doc == null) throw new IllegalArgumentException("Document parent and path must not be null"); Element e = parent; for (int i = 0; i < path.length; i++) { Element newEl = getChildElementByTagName(e, path[i]); if (newEl == null) { newEl = doc.createElement(path[i]); e.appendChild(newEl); } e = newEl; } return e; }
From source file:Main.java
public static void stripDuplicateAttributes(Node node, Node parent) { // The output depends on the type of the node switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document doc = (Document) node; Node child = doc.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); }/*from w ww. jav a2 s. com*/ break; } case Node.ELEMENT_NODE: { Element elt = (Element) node; NamedNodeMap attrs = elt.getAttributes(); ArrayList nodesToRemove = new ArrayList(); int nodesToRemoveNum = 0; for (int i = 0; i < attrs.getLength(); i++) { final Node a = attrs.item(i); for (int j = 0; j < attrs.getLength(); j++) { final Node b = attrs.item(j); //if there are two attributes with same name if (i != j && (a.getNodeName().equals(b.getNodeName()))) { nodesToRemove.add(b); nodesToRemoveNum++; } } } for (int i = 0; i < nodesToRemoveNum; i++) { Attr nodeToDelete = (Attr) nodesToRemove.get(i); Element nodeToDeleteParent = (Element) node; // nodeToDelete.getParentNode(); nodeToDeleteParent.removeAttributeNode(nodeToDelete); } nodesToRemove.clear(); Node child = elt.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); } break; } default: //do nothing break; } }
From source file:Main.java
public static <T> Map<String, T> loadBeans(InputStream stream, Class<T> beanClass) throws SAXException, IOException, ParserConfigurationException, IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException, DOMException, InvocationTargetException { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream); return loadBeans(document.getFirstChild(), beanClass); }
From source file:Main.java
/** @param doc an XML document. * @return a string representing the stringified version of that document, with minor pretty-printing. */// w ww. j ava 2 s . c o m public static String doc2String(Document doc) { //System.out.println("XMLUtils: converting doc to string"); StringBuffer result = new StringBuffer(); doc2String(doc.getFirstChild(), result, 0); return result.toString(); }
From source file:org.datacleaner.util.http.HttpXmlUtils.java
public static Element getRootNode(HttpClient httpClient, String url) throws InvalidHttpResponseException { logger.info("getRootNode({})", url); try {/* w w w .j a v a 2 s. co m*/ HttpGet method = new HttpGet(url); HttpResponse response = httpClient.execute(method); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { logger.error("Response status code was: {} (url={})", statusCode, url); throw new InvalidHttpResponseException(url, response); } InputStream inputStream = response.getEntity().getContent(); Document document = createDocumentBuilder().parse(inputStream); return (Element) document.getFirstChild(); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new IllegalStateException("Could not get root XML node of url=" + url, e); } }
From source file:ProcessorDemo.java
private static void readConfig(String confFile) { String parent = new File(confFile).getParentFile().getParent(); try {// ww w .jav a 2 s .c o m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(confFile)); NodeList nl = doc.getFirstChild().getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { org.w3c.dom.Node n = nl.item(i); if (n.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { String nn = n.getNodeName(); String value = n.getFirstChild().getNodeValue(); if (nn.equals("composit")) { compositRule += value + "\n"; } if (nn.equals("compound")) { if (value.equals("\u69cb\u6210\u8a9e")) { isCompound = false; } } if (nn.equals("remark")) { remarkRule += value + "\n"; } if (nn.equals("dictionary")) { // read nested tag in <dictinary> NodeList dnl = n.getChildNodes(); for (int j = 0; j < dnl.getLength(); j++) { org.w3c.dom.Node dn = dnl.item(j); if (dn.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { String dnn = dn.getNodeName(); if (dn.getFirstChild() == null) { throw new IllegalArgumentException("element '" + dnn + "' is empty"); } String dvalue = dn.getFirstChild().getNodeValue(); if (dnn.equals("compound")) { compoundFile = SenUtils.getPath(dvalue, parent); } } } } } } if (!isCompound) { try { ObjectInputStream is = new ObjectInputStream(new FileInputStream(compoundFile)); HashMap hashmap = (HashMap) is.readObject(); } catch (ClassNotFoundException e1) { throw new RuntimeException(e1); } } } catch (ParserConfigurationException e) { throw new IllegalArgumentException(e.getMessage()); } catch (FileNotFoundException e) { throw new IllegalArgumentException(e.getMessage()); } catch (SAXException e) { throw new IllegalArgumentException(e.getMessage()); } catch (IOException e) { throw new IllegalArgumentException(e.getMessage()); } }
From source file:de.ingrid.iplug.csw.dsc.TestUtil.java
private static CSWRecord getRecordNode(String id, ElementSetName elementSetName, CSWRecord record) throws Exception { File file = new File( dataFolder + "/" + FileUtils.encodeFileName(id) + "_" + elementSetName.toString() + ".xml"); StringBuilder content = new StringBuilder(); BufferedReader input = new BufferedReader(new FileReader(file)); try {//from ww w. ja v a 2s. c om String line = null; while ((line = input.readLine()) != null) { content.append(line); content.append(System.getProperty("line.separator")); } input.close(); input = null; Document document = StringUtils.stringToDocument(content.toString()); record.initialize(elementSetName, document.getFirstChild()); return record; } finally { if (input != null) input.close(); } }