我的工具中有一个方法,它将XML String作为条目:
"<file type=\"JNH\"> " +
"<field code=\"AAAAA\" name=\"NAME1\">string1</field> " +
"<field code=\"BBBBB\" name=\"NAME2\">string2</field> " +
"<desc code=\"CCCC\" name=\"DESC\"> " +
"<txt>bla.</txt> " +
"<txt>bla.</txt> " +
"</desc> " +
"</file> "
主要是格式,实际上我的工具需要XML字符串输入,但我想在我的应用程序中自动构建这个XML字符串。我的意思是代替myService(String xml)
,我想要myService(String string1, String string2, String[] txt)
。
这样做的最佳解决方案是什么?使用.replaceAll()
方法执行XML模板? (文本行数可以更改)
非常感谢。
答案 0 :(得分:0)
我的示例代码在这里创建了xml字符串。我希望这可以帮到你。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateXMLClass {
public CreateXMLClass() {
String xmlString = createXML("value1", "value2", "value3");
System.out.println(xmlString);
}
// creates xml and returns the content
private String createXML(String v1, String v2, String v3) {
String data="";
TransformerFactory transformerFactory;
Transformer transformer;
DOMSource source;
StreamResult result;
StringWriter writer;
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root elements
Document doc = docBuilder.newDocument();
Element soapEnvelope = doc.createElement("soapenv:Envelope");
soapEnvelope.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
soapEnvelope.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
soapEnvelope.setAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
soapEnvelope.setAttribute("xmlns:ns0", "https://ws.mycompany.com/");
doc.appendChild(soapEnvelope);
//elements
Element header = doc.createElement("soapenv:Header");
soapEnvelope.appendChild(header);
Element soapBody = doc.createElement("soapenv:Body");
soapEnvelope.appendChild(soapBody);
Element ns0 = doc.createElement("ns0:SampleApplication");
ns0.setAttribute("xmlns:ns0", "http://ws.mycompany.com");
soapBody.appendChild(ns0);
Element element01 = doc.createElement("Sample1");
element01.appendChild(doc.createTextNode(v1));
ns0.appendChild(element01);
Element element02 = doc.createElement("Sample2");
element02.appendChild(doc.createTextNode(v2));
ns0.appendChild(element02);
Element element03 = doc.createElement("Sample3");
element03.appendChild(doc.createTextNode(v2));
ns0.appendChild(element03);
writer = new StringWriter();
result = new StreamResult(writer);
transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
source = new DOMSource(doc);
transformer.transform(source, result);
data = writer.getBuffer().toString();
} catch(ParserConfigurationException pce){
pce.printStackTrace();
} catch(TransformerException tfe){
tfe.printStackTrace();
}
return data;
}
public static void main(String args[]) {
new CreateXMLClass();
System.exit(0);
}
}
答案 1 :(得分:0)
不要使用字符串创建XML - 它很容易输入错误(忘记关闭标记,忘记转义引号)。使用javax.xml
包中的标准API。它看起来像这样:
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Test {
public String getXMLString(String string1, String string2, String[] txts) throws Exception {
// Create document
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
// Root element
Element rootElement = doc.createElement("file");
rootElement.setAttribute("type", "JNH");
doc.appendChild(rootElement);
// Field 1
Element field1Element = doc.createElement("field");
field1Element.setAttribute("code", "AAAAA");
field1Element.setAttribute("name", "NAME1");
field1Element.setTextContent(string1);
rootElement.appendChild(field1Element);
// likewise for Field 2
// Desc
Element descElement = doc.createElement("desc");
descElement.setAttribute("code", "CCCC");
descElement.setAttribute("name", "DESC");
descElement.setTextContent(string1);
rootElement.appendChild(descElement);
// Children
for (String txt : txts) {
Element txtElement = doc.createElement("desc");
txtElement.setTextContent(txt);
descElement.appendChild(txtElement);
}
// Save document as string
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
return writer.getBuffer().toString();
}
}