List of usage examples for javax.xml.parsers DocumentBuilder newDocument
public abstract Document newDocument();
From source file:XMLUtils.java
/** * Creates a new and empty document.//from w ww .j ava 2s. c o m * @return */ public static Document newDocument() { DocumentBuilder builder = null; try { builder = getParser(); return builder.newDocument(); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } finally { releaseParser(builder); } }
From source file:eu.europeana.uim.sugarcrmclient.internal.helpers.ClientUtils.java
/** * Creates a SelectFields Soap Object given a List<String> fieldnames object * which sets the fields to be retrieved. * /*from w w w.java2 s. c om*/ * @param fieldnames * @return */ public static SelectFields generatePopulatedSelectFields(List<String> fieldnames) { SelectFields selfields = new SelectFields(); StringBuffer arrayType = new StringBuffer(); arrayType.append("string["); arrayType.append(fieldnames.size()); arrayType.append("]"); CommonAttributes commonAttributes = new CommonAttributes(); commonAttributes.setHref(arrayType.toString()); selfields.setCommonAttributes(commonAttributes); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); for (String fieldname : fieldnames) { Element element = document.createElement("string"); Array array = new Array(); array.getAnyList(); selfields.setArray(array); element.appendChild(document.createTextNode(fieldname)); } } catch (ParserConfigurationException e) { e.printStackTrace(); return null; } return selfields; }
From source file:com.bluexml.side.form.utils.DOMUtil.java
/** * Returns a new empty document./* w ww . ja v a2s . com*/ */ public static Document getNewDocument() { DocumentBuilder docBuilder = getDocumentBuilder(); if (docBuilder == null) { return null; } Document doc = docBuilder.newDocument(); return doc; }
From source file:XMLUtils.java
/** * Creates a new document with a document root element. * @param name//from www.j ava2 s. com * @return */ public static Element newElement(String name) { DocumentBuilder builder = null; try { builder = getParser(); Document doc = builder.newDocument(); Element el = doc.createElement(name); doc.appendChild(el); return el; } catch (ParserConfigurationException e) { throw new RuntimeException(e); } finally { releaseParser(builder); } }
From source file:eu.europeana.uim.sugarcrmclient.internal.helpers.ClientUtils.java
/** * Creates a NameValueList Soap Element given a List<String> namevalues * object which sets the fields to be retrieved. * // w ww . j a v a2 s. c o m * @param fieldnames * @return */ public static NameValueList generatePopulatedNameValueList(List<NameValue> namevalues) { NameValueList namevalueList = new NameValueList(); StringBuffer arrayType = new StringBuffer(); arrayType.append("name_value["); arrayType.append(namevalues.size()); arrayType.append("]"); Array array = new Array(); ArrayType arrTypeObj = new ArrayType(); arrTypeObj.setArrayType(arrayType.toString()); ArrayAttributes atts = new ArrayAttributes(); atts.setArrayType(arrTypeObj); namevalueList.setArrayAttributes(atts); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); for (NameValue namevalue : namevalues) { Element name_value = document.createElement("name_value"); Element name = document.createElement("name"); Element value = document.createElement("value"); name.appendChild(document.createTextNode(namevalue.getName())); value.appendChild(document.createTextNode(namevalue.getValue())); name_value.appendChild(name); name_value.appendChild(value); array.getAnyList().add(name_value); } namevalueList.setArray(array); } catch (ParserConfigurationException e) { e.printStackTrace(); return null; } return namevalueList; }
From source file:com.ephesoft.dcma.util.XMLUtil.java
/** * To create new Document./*from ww w . j a v a2s . co m*/ * * @return Document * @throws ParserConfigurationException */ public static Document createNewDocument() throws ParserConfigurationException { DocumentBuilder builder = getBuilder(); return builder.newDocument(); }
From source file:com.sinet.gage.dlap.utils.XMLUtils.java
/** * @param String//from w ww . j a v a2 s. c om * @param String * @return String */ public static String parseXML(String xml, String rootElementName) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); DocumentBuilder db; db = dbf.newDocumentBuilder(); Document newDoc = db.parse(new ByteArrayInputStream(xml.getBytes())); Element element = (Element) newDoc.getElementsByTagName(rootElementName).item(0); // Imports a node from another document to this document, // without altering Document newDoc2 = db.newDocument(); // or removing the source node from the original document Node copiedNode = newDoc2.importNode(element, true); // Adds the node to the end of the list of children of this node newDoc2.appendChild(copiedNode); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(newDoc2), new StreamResult(out)); return out.toString(); } catch (TransformerException | ParserConfigurationException | SAXException | IOException e) { LOGGER.error("Exception in parsing xml from response: ", e); } return ""; }
From source file:com.adaptris.core.util.XmlHelper.java
/** * Create a document from an AdaptrisMessage. * @param s the string containing XML/*w w w . j a va2s.co m*/ * @param builder configuration for the underlying {@link DocumentBuilderFactory} instance.. * @param newDocOnFailure return a new document if the String is not XML. * @return the Document element */ public static Document createDocument(String s, DocumentBuilderFactoryBuilder builder, boolean newDocOnFailure) throws ParserConfigurationException, IOException, SAXException { Document result = null; DocumentBuilder docBuilder = newDocumentBuilder(builder); try (StringReader in = new StringReader(s)) { result = docBuilder.parse(new InputSource(in)); } catch (IOException | SAXException e) { if (newDocOnFailure) { result = docBuilder.newDocument(); } else { throw e; } } return result; }
From source file:com.google.visualization.datasource.render.HtmlRenderer.java
/** * Creates a document element.// w ww .j a v a 2s. c o m * * @return A document element. */ private static Document createDocument() { DocumentBuilder documentBuilder = null; try { documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { log.error("Couldn't create a document builder", e); throw new RuntimeException("Couldn't create a document builder. This should never happen.", e); } Document document = documentBuilder.newDocument(); return document; }
From source file:com.zimbra.cs.service.AutoDiscoverServlet.java
private static String createResponseDoc(String displayName, String email, String serviceUrl) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/* w w w .ja v a 2 s. c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document xmlDoc = builder.newDocument(); Element root = xmlDoc.createElementNS(NS, "Autodiscover"); root.setAttribute("xmlns", NS); root.setAttribute("xmlns:xsi", XSI_NS); root.setAttribute("xmlns:xsd", XSD_NS); xmlDoc.appendChild(root); //Add the response element. Element response = xmlDoc.createElementNS(NS_MOBILE, "Response"); root.appendChild(response); //Add culture to to response Element culture = xmlDoc.createElement("Culture"); culture.appendChild(xmlDoc.createTextNode("en:en")); response.appendChild(culture); //User Element user = xmlDoc.createElement("User"); Element displayNameElm = xmlDoc.createElement("DisplayName"); displayNameElm.appendChild(xmlDoc.createTextNode(displayName)); user.appendChild(displayNameElm); Element emailAddr = xmlDoc.createElement("EMailAddress"); emailAddr.appendChild(xmlDoc.createTextNode(email)); user.appendChild(emailAddr); response.appendChild(user); //Action Element action = xmlDoc.createElement("Action"); Element settings = xmlDoc.createElement("Settings"); Element server = xmlDoc.createElement("Server"); Element type = xmlDoc.createElement("Type"); type.appendChild(xmlDoc.createTextNode("MobileSync")); server.appendChild(type); Element url = xmlDoc.createElement("Url"); url.appendChild(xmlDoc.createTextNode(serviceUrl)); server.appendChild(url); Element name = xmlDoc.createElement("Name"); name.appendChild(xmlDoc.createTextNode(serviceUrl)); server.appendChild(name); settings.appendChild(server); action.appendChild(settings); response.appendChild(action); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(xmlDoc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); writer.flush(); String xml = writer.toString(); writer.close(); //manually generate xmlns for Autodiscover and Response element, this works //for testexchangeconnectivity.com, but iOS and Android don't like Response's xmlns // StringBuilder str = new StringBuilder(); // str.append("<?xml version=\"1.0\"?>\n"); // str.append("<Autodiscover xmlns:xsd=\"").append(XSD_NS).append("\""); // str.append(" xmlns:xsi=\"").append(XSI_NS).append("\""); // str.append(" xmlns=\"").append(NS).append("\">\n"); // int respIndex = xml.indexOf("<Response>"); // str.append("<Response xmlns=\"").append(NS_MOBILE).append("\">"); // str.append(xml.substring(respIndex + "<Response>".length(), xml.length())); // return str.toString(); return "<?xml version=\"1.0\"?>\n" + xml; }