List of usage examples for org.w3c.dom Document getDocumentElement
public Element getDocumentElement();
From source file:Main.java
License:asdf
public static void newEmail(Document doc, String newname, String newemail) { Element root = doc.getDocumentElement(); NodeList rootlist = root.getChildNodes(); for (int i = 0; i < rootlist.getLength(); i++) { Element person = (Element) rootlist.item(i); NodeList personlist = person.getChildNodes(); Element name = (Element) personlist.item(0); NodeList namelist = name.getChildNodes(); Text nametext = (Text) namelist.item(0); String oldname = nametext.getData(); if (oldname.equals(newname)) { Element email = (Element) personlist.item(1); NodeList emaillist = email.getChildNodes(); Text emailtext = (Text) emaillist.item(0); emailtext.setData(newemail); }// w w w. j a va2 s . com } }
From source file:Main.java
public static void createComment(Document doc, String comment) { Comment c = doc.createComment(comment); doc.getDocumentElement().appendChild(c); }
From source file:Main.java
public static NodeList getXMLNodes(final String xmlFileName, final String tagName) { NodeList nList = null;//w ww.j ava2 s. c o m try { File xmlFile = new File(xmlFileName); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); doc.getDocumentElement().normalize(); nList = doc.getElementsByTagName(tagName); } catch (Exception e) { e.printStackTrace(); } return nList; }
From source file:Main.java
public static Element stringToElement(String str) { StringReader sr = new StringReader(str); try {// w ww. j av a 2s . c o m DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(false); docBuilderFactory.setValidating(false); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); InputSource is = new InputSource(sr); // is.setEncoding("gb2312"); Document doc = docBuilder.parse(is); return doc.getDocumentElement(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static Document addRoot(Document dataDoc, String elementName) { Element oldRoot = dataDoc.getDocumentElement(); Element newRoot = dataDoc.createElement(elementName); dataDoc.removeChild(oldRoot);/*from w ww . ja v a 2 s . c o m*/ newRoot.appendChild(oldRoot); dataDoc.appendChild(newRoot); return dataDoc; }
From source file:Main.java
/** * Makes a DOM document out of an XML file. * @param pathToFile path to the XML file. * @return a DOM document//from w w w . j a v a 2 s . c om */ public static Document parseXmlFile(String pathToFile) { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pathToFile); doc.getDocumentElement().normalize(); return doc; } catch (final ParserConfigurationException e) { throw new RuntimeException(e); // TODO proper exception handling } catch (final SAXException e) { throw new RuntimeException(e); // TODO proper exception handling } catch (final IOException e) { throw new RuntimeException(e); // TODO proper exception handling } }
From source file:Main.java
public static Document getXMLDocument(String path) throws SAXException, IOException, ParserConfigurationException { Document d = getDocumentBuilder().parse(new File(path)); d.getDocumentElement().normalize(); doneParsing();/* www. j a v a 2 s.c o m*/ return d; }
From source file:Main.java
public static Document loadXml(File sourceFile) { try {/*from w w w . j av a2 s . c o m*/ DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document baseSizeDoc = dBuilder.parse(sourceFile); baseSizeDoc.getDocumentElement().normalize(); return baseSizeDoc; } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } throw new IllegalStateException("Could not load xml file: " + sourceFile.getPath()); }
From source file:Main.java
/** * Transforms a string representation to a DOM representation * @param xmlString XML as string//from ww w . j ava 2s. co m * @return DOM representation of String * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Element stringToDOM(String xmlString) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); Reader reader = new StringReader(xmlString); InputSource src = new InputSource(reader); Document domDoc = builder.parse(src); return domDoc.getDocumentElement(); }
From source file:Utils.java
/** * Copy a Node from one source document, adding it to the document * root of a different, target Document/*w w w . j a v a 2 s .co m*/ * @param source Document to copy * @param target Document to contain copy */ public static void copyDocumentNode(Node source, Document target) { Node node = target.importNode(source, true); target.getDocumentElement().appendChild(node); }