List of usage examples for org.w3c.dom Document createElement
public Element createElement(String tagName) throws DOMException;
From source file:Utils.java
public static Element findContainerWithAttributeValueElseCreate(Document document, Element parent, String element, String attributeName, String attributeValue) { NodeList nl = parent.getElementsByTagName(element); Element e;/*ww w . j a v a2 s . co m*/ for (int i = 0; i < nl.getLength(); i++) { e = (Element) nl.item(i); if (e.getAttribute(attributeName).equals(attributeValue)) { return e; } } e = document.createElement(element); parent.appendChild(e); e.setAttribute(attributeName, attributeValue); return e; }
From source file:at.gv.egovernment.moa.id.util.client.mis.simple.MISSimpleClient.java
private static Element packIntoSOAP(Element element) throws MISSimpleClientException { try {/*from ww w. j a v a2s.c om*/ Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element soapEnvelope = doc.createElement("Envelope"); soapEnvelope.setAttribute("xmlns", SOAP_NS); Element soapBody = doc.createElement("Body"); soapEnvelope.appendChild(soapBody); soapBody.appendChild(doc.importNode(element, true)); return soapEnvelope; } catch (ParserConfigurationException e) { throw new MISSimpleClientException("service.06", e); } }
From source file:com.nridge.core.base.std.XMLUtl.java
public static void makeElemDoubleValue(Document aDocument, Element anElement, String aName, double aValue) { Element subElement;//from www. j a v a 2s .co m Double doubleObject; if (StringUtils.isNotEmpty(aName)) { doubleObject = aValue; subElement = aDocument.createElement(aName); subElement.appendChild(aDocument.createTextNode(doubleObject.toString())); anElement.appendChild(subElement); } }
From source file:com.mirth.connect.connectors.http.HttpMessageConverter.java
public static String httpResponseToXml(String status, Map<String, List<String>> headers, Object content, ContentType contentType, boolean parseMultipart, boolean includeMetadata, BinaryContentTypeResolver resolver) { try {/*w w w . j a va 2 s .co m*/ Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); DonkeyElement requestElement = new DonkeyElement(document.createElement("HttpResponse")); if (includeMetadata) { requestElement.addChildElement("Status", status); DonkeyElement headerElement = requestElement.addChildElement("Header"); for (Entry<String, List<String>> entry : headers.entrySet()) { for (String value : entry.getValue()) { DonkeyElement fieldElement = headerElement.addChildElement("Field"); fieldElement.addChildElement("Name", entry.getKey()); fieldElement.addChildElement("Value", value); } } } processContent(requestElement.addChildElement("Body"), content, contentType, parseMultipart, resolver); return requestElement.toXml(); } catch (Exception e) { logger.error("Error converting HTTP request.", e); } return null; }
From source file:apps.ParsedPost.java
private static String createYahooAnswersQuestion(ParsedPost parentPost, ParsedPost post) throws ParserConfigurationException, TransformerException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("document"); doc.appendChild(rootElement);//from www . j a va 2 s.c o m Element uri = doc.createElement("uri"); uri.setTextContent(parentPost.mId); rootElement.appendChild(uri); Element subject = doc.createElement("subject"); subject.setTextContent(parentPost.mTitle); rootElement.appendChild(subject); Element content = doc.createElement("content"); content.setTextContent(parentPost.mBody); rootElement.appendChild(content); Element bestanswer = doc.createElement("bestanswer"); bestanswer.setTextContent(post.mBody); rootElement.appendChild(bestanswer); Element answer_item = doc.createElement("answer_item"); answer_item.setTextContent(post.mBody); Element nbestanswers = doc.createElement("nbestanswers"); nbestanswers.appendChild(answer_item); rootElement.appendChild(nbestanswers); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return "<vespaadd>" + xhlp.removeHeader(sw.toString()).replace("&", "&") + "</vespaadd>\n"; }
From source file:com.mirth.connect.connectors.http.HttpMessageConverter.java
public static String httpRequestToXml(HttpRequestMessage request, boolean parseMultipart, boolean includeMetadata, BinaryContentTypeResolver resolver) { try {/*from w ww . j a v a 2 s . c o m*/ Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); DonkeyElement requestElement = new DonkeyElement(document.createElement("HttpRequest")); if (includeMetadata) { requestElement.addChildElement("RemoteAddress", request.getRemoteAddress()); requestElement.addChildElement("RequestUrl", request.getRequestUrl()); requestElement.addChildElement("Method", request.getMethod()); requestElement.addChildElement("RequestPath", request.getQueryString()); requestElement.addChildElement("RequestContextPath", request.getContextPath()); if (!request.getParameters().isEmpty()) { DonkeyElement parametersElement = requestElement.addChildElement("Parameters"); for (Entry<String, List<String>> entry : request.getParameters().entrySet()) { for (String value : entry.getValue()) { parametersElement.addChildElement(entry.getKey(), value); } } } DonkeyElement headerElement = requestElement.addChildElement("Header"); for (Entry<String, List<String>> entry : request.getHeaders().entrySet()) { for (String value : entry.getValue()) { headerElement.addChildElement(entry.getKey(), value); } } } processContent(requestElement.addChildElement("Content"), request.getContent(), request.getContentType(), parseMultipart, resolver); return requestElement.toXml(); } catch (Exception e) { logger.error("Error converting HTTP request.", e); } return null; }
From source file:Main.java
/** * Adds a new node to a file.//from w w w. j a va 2s . com * * @param nodeType {@link String} The type of the element to add. * @param idField {@link String} The name of the field used to identify this * node. * @param nodeID {@link String} The identifier for this node, so its data * can be later retrieved and modified. * @param destFile {@link File} The file where the node must be added. * @param attributes {@link ArrayList} of array of String. The arrays must * be bidimensional (first index must contain attribute name, second one * attribute value). Otherwise, an error will be thrown. However, if * <value>null</value>, it is ignored. */ public static void addNode(String nodeType, String idField, String nodeID, File destFile, ArrayList<String[]> attributes) { if (attributes != null) { for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) { if (it.next().length != 2) { throw new IllegalArgumentException("Invalid attribute combination"); } } } /* * XML DATA CREATION - BEGINNING */ DocumentBuilder docBuilder; Document doc; try { docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = docBuilder.parse(destFile); } catch (SAXException | IOException | ParserConfigurationException ex) { return; } Node index = doc.getFirstChild(), newElement = doc.createElement(nodeType); NamedNodeMap elementAttributes = newElement.getAttributes(); Attr attrID = doc.createAttribute(idField); attrID.setValue(nodeID); elementAttributes.setNamedItem(attrID); if (attributes != null) { for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) { String[] x = it.next(); Attr currAttr = doc.createAttribute(x[0]); currAttr.setValue(x[1]); elementAttributes.setNamedItem(currAttr); } } index.appendChild(newElement); /* * XML DATA CREATION - END */ /* * XML DATA DUMP - BEGINNING */ Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException ex) { return; } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); try { transformer.transform(source, result); } catch (TransformerException ex) { return; } String xmlString = result.getWriter().toString(); try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFile))) { bufferedWriter.write(xmlString); } catch (IOException ex) { } /* * XML DATA DUMP - END */ }
From source file:com.google.visualization.datasource.render.HtmlRenderer.java
/** * Renders a simple html for the given responseStatus. * * @param responseStatus The response message. * * @return A simple html for the given responseStatus. *///from w w w. j a v a 2s .c o m public static CharSequence renderHtmlError(ResponseStatus responseStatus) { // Get the responseStatus details. StatusType status = responseStatus.getStatusType(); ReasonType reason = responseStatus.getReasonType(); String detailedMessage = responseStatus.getDescription(); // Create an xml document with head and an empty body. Document document = createDocument(); Element bodyElement = appendHeadAndBody(document); // Populate the xml document. Element oopsElement = document.createElement("h3"); oopsElement.setTextContent("Oops, an error occured."); bodyElement.appendChild(oopsElement); if (status != null) { String text = "Status: " + status.lowerCaseString(); appendSimpleText(document, bodyElement, text); } if (reason != null) { String text = "Reason: " + reason.getMessageForReasonType(null); appendSimpleText(document, bodyElement, text); } if (detailedMessage != null) { String text = "Description: " + sanitizeDetailedMessage(detailedMessage); appendSimpleText(document, bodyElement, text); } return transformDocumentToHtmlString(document); }
From source file:Main.java
/** * Creates (only if necessary) and returns the element which is at the end of the specified * path.//from ww w. j a v a2 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:fr.ece.epp.tools.Utils.java
public static void updateProduct(String path, String[] feature, boolean outOrno) { Document document = null; try {//from w w w .ja v a 2s .c o m document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path); Element root = document.getDocumentElement(); Node features = root.getElementsByTagName("features").item(0); Element basefeature = document.createElement("feature"); basefeature.setAttribute("id", "org.eclipse.platform"); features.appendChild(basefeature); for (int i = 0; i < feature.length; i++) { if (feature[i] != null && feature[i].trim().length() > 0) { Element fea = document.createElement("feature"); if (feature[i].endsWith(".feature.group")) { int count = feature[i].length() - ".featyre.group".length(); String id = feature[i].substring(0, count); fea.setAttribute("id", id); } else { fea.setAttribute("id", feature[i]); } features.appendChild(fea); } } output(root, path); if (outOrno) { output(root, null); } } catch (SAXException e) { } catch (IOException e) { } catch (ParserConfigurationException e) { } }