List of usage examples for org.w3c.dom Document getDocumentElement
public Element getDocumentElement();
From source file:Main.java
public static Document getXMLFromURL(String url) { Document xmlDoc = null; try {/* w w w . j a va 2s .c om*/ docBuilder = builderFactory.newDocumentBuilder(); xmlDoc = docBuilder.parse(url); } catch (ParserConfigurationException error) { System.err.println("Error reading parser configuration."); return xmlDoc; } catch (SAXException error) { System.err.println("Error parsing document."); return xmlDoc; } catch (IOException error) { System.err.println("Error accessing URL."); return xmlDoc; } xmlDoc.getDocumentElement().normalize(); return xmlDoc; }
From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java
/** * @param commentsDom/*from ww w.j av a 2 s. c o m*/ * @param commentTemplate * @param messageText * @param commentId */ static void addCommentToComments(Document commentsDom, Element commentTemplate, String messageText, String commentId) { Element comment = (Element) commentsDom.importNode(commentTemplate, true); commentsDom.getDocumentElement().appendChild(comment); comment.setAttributeNS(wNs, "w:id", commentId); comment.setAttributeNS(wNs, "w:author", "XML Validator"); comment.setAttributeNS(wNs, "w:initials", "XMLVal"); comment.setAttributeNS(wNs, "w:date", timestampFormatter.format(Calendar.getInstance().getTime())); Element elem = DataUtil.getElementNS(comment, wNs, "p"); NodeList nl = elem.getElementsByTagNameNS(wNs, "r"); elem = (Element) nl.item(nl.getLength() - 1); Element text = DataUtil.getElementNS(elem, wNs, "t"); text.setTextContent(messageText); }
From source file:com.viettel.ws.client.JDBCUtil.java
/** * Create document using DOM api//from ww w. j a v a 2 s . com * * @param rs a result set * @param doc a input document for append content * @param rsName name of the appended element * @return a document after append content * @throws ParserConfigurationException If error when parse XML string * @throws SQLException If error when read data from database */ public static Document add2Document1(ResultSet rs1, ResultSet rs2, Document doc, String rsName) throws ParserConfigurationException, SQLException { if (rs1 == null && rs2 == null) { return doc; } //Get root element Element root = doc.getDocumentElement(); Element rsElement = doc.createElement(rsName); root.appendChild(rsElement); if (rs1 != null) { ResultSetMetaData rsmd = rs1.getMetaData(); int colCount = rsmd.getColumnCount(); while (rs1.next()) { Element row = doc.createElement("Row"); rsElement.appendChild(row); try { for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs1.getObject(i); if (value == null) { value = ""; } Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } catch (Exception ex) { LogUtil.addLog(ex);//binhnt sonar a160901 // logger.error(e, e); } } } if (rs2 != null) { ResultSetMetaData rsmd = rs2.getMetaData(); int colCount = rsmd.getColumnCount(); while (rs2.next()) { Element row = doc.createElement("Row"); rsElement.appendChild(row); try { for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs2.getObject(i); if (value == null) { value = ""; } Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } catch (Exception ex) { LogUtil.addLog(ex);//binhnt sonar a160901 // logger.error(e, e); } } } return doc; }
From source file:com.googlecode.jgenhtml.JGenHtmlUtils.java
public static void linkToXsl(Document doc, final String xslPath) { ProcessingInstruction xsltLink = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + xslPath + '"'); Element documentElement = doc.getDocumentElement(); doc.insertBefore(xsltLink, documentElement); }
From source file:edu.stanford.muse.webapp.Sessions.java
public static boolean parseArchivesXml(String xmlFile) { if (!(new File(xmlFile)).exists()) return false; if (archivesMap != null) // can't reload (any other) archives.xml return true; boolean result = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); org.w3c.dom.Document dom; try {/*ww w . j ava 2 s .c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); dom = db.parse(xmlFile); Element e_root = dom.getDocumentElement(); // get a nodelist of <item> elements NodeList nl = e_root.getElementsByTagName("item"); assert (nl != null); archivesMap = new LinkedHashMap<String, Map<String, String>>(); for (int i = 0; i < nl.getLength(); i++) { // get an <item> elemment Element e = (Element) nl.item(i); Map<String, String> map = new LinkedHashMap<String, String>(); // extract following tags addXmlTagToMap(map, e, "name"); addXmlTagToMap(map, e, "number"); addXmlTagToMap(map, e, "description"); addXmlTagToMap(map, e, "file"); addXmlOptionalTagToMap(map, e, "lexicon"); addXmlOptionalTagToMap(map, e, "findingaids"); addXmlOptionalTagToMap(map, e, "searchworks"); log.info("Added archive " + map); archivesMap.put(map.get("number"), map); } } catch (Exception e) { e.printStackTrace(); result = false; } return result; }
From source file:Main.java
public static void saveXml(Document dom, File file, boolean omitXmlDeclaration) throws IOException { try {//from w w w. j a va2s.c om Transformer transformer = getNewTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); transformer.transform(new DOMSource(dom.getDocumentElement()), new StreamResult(file.toURI().getPath())); } catch (Exception e) { throw new IOException("saveXml failed because : " + e.getMessage()); } }
From source file:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java
public static org.w3c.dom.Element marshal(Object obj) throws JAXBException { try {/*from w ww. j av a 2s. c o m*/ Document doc = null; JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = jc.createMarshaller(); javax.xml.parsers.DocumentBuilderFactory dbf; dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); doc = dbf.newDocumentBuilder().newDocument(); marshaller.marshal(obj, doc); log.info("Marshal: " + doc.getNodeValue()); return doc.getDocumentElement(); } catch (ParserConfigurationException ex) { throw new JAXBException("Unable to create document: " + ex.getMessage()); } }
From source file:com.vmware.demo.SamlUtils.java
/** * Parse an XML string into an XMLObject * @param messageXML/*from w ww . j a v a2 s. c om*/ * @return * @throws Exception * @throws SamlException */ public static XMLObject unmarshallMessage(String messageXML) throws SamlException { try { ByteArrayInputStream bais = new ByteArrayInputStream(messageXML.getBytes(SamlGenerator.CHARSET_UTF8)); ParserPool parserPool = new BasicParserPool(); Document messageDoc = parserPool.parse(bais); Element messageElem = messageDoc.getDocumentElement(); Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem); if (unmarshaller == null) { throw new SamlException("Unable to unmarshall message, no unmarshaller registered for element " + XMLHelper.getNodeQName(messageElem)); } return unmarshaller.unmarshall(messageElem); } catch (XMLParserException e) { throw new SamlException("Unable to parse message into a DOM", e); } catch (UnmarshallingException e) { throw new SamlException("Unable to unmarshall message from its DOM", e); } }
From source file:Pathway2Rdf.java
/** * @param args// w w w. ja v a 2s.co m * @throws XPathExpressionException * @throws DOMException * @throws ServiceException * @throws ConverterException * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws ParseException */ private static boolean isValidXML(String gpml, String wpIdentifier, String revision) { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); StringReader reader = new StringReader(gpml); InputSource inputSource = new InputSource(reader); Document doc = docBuilder.parse(inputSource); reader.close(); doc.getDocumentElement().normalize(); return true; } catch (SAXParseException spe) { System.out.println("Invalid GPML: " + wpIdentifier + "/" + revision); return false; } catch (SAXException sxe) { System.out.println("Invalid GPML: " + wpIdentifier + "/" + revision); return false; } catch (ParserConfigurationException pce) { System.out.println("Invalid GPML: " + wpIdentifier + "/" + revision); return false; } catch (IOException ioe) { System.out.println("Invalid GPML: " + wpIdentifier + "/" + revision); return false; } }