我想借助java代码将一个xml转换为另一个xml。我想给一个xml作为输入文件,并期望另一个xml文件作为输出。我们如何在java的帮助下做这件事?任何人都可以向我提出建议。
Input.xml文件
<Order OrderNo=”1234567890”>
<OrderLines>
<OrderLine PrimeLineNo=”1” SubLineNo=”1”/>
<OrderLine PrimeLineNo=”2” SubLineNo=”1”/>
</OrderLines>
</Order>
output.xml文件我需要像这样输出这个文件
<Order OrderName="1234567890">
<OrderLines MaxOrderNumbers=”2”>
<OrderLine LineNumber="1" SubLineNumber="1"/>
<OrderLine LineNumber ="2" SubLineNumber ="1"/>
</OrderLines>
</Order>
但我已经在java的帮助下尝试了下面的代码。
public class XmlToXml {
public static void main(String[] args) {
final String xmlStr ="<Order OrderNo=\"1234567890\"><OrderLines><OrderLine PrimeLineNo=\"1\" SubLineNo=\"1\"/><OrderLine PrimeLineNo=\"2\" SubLineNo=\"1\"/></OrderLines></Order>";
Document doc = convertStringToDocument(xmlStr);
String str = convertDocumentToString(doc);
System.out.println(str);
}
private static String convertDocumentToString(Document doc) {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tf.newTransformer();
System.out.println(transformer.getParameter("xmlStr"));
StringWriter writer = new StringWriter();
writer.append("MaxOrderNumbers");
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString();
return output;
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}
private static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource( new StringReader( xmlStr ) ) );
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:1)
解决这个问题的方法是在XSLT中定义转换规则。
默认情况下,您需要一个可以复制内容的规则:
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
然后您需要进一步的规则来定义您想要进行的更改:
<xsl:template match="@OrderNo">
<xsl:attribute name="OrderName"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
和其他重命名规则类似,加上
<xsl:template match="OrderLines">
<OrderLines MaxOrderLines="{count(*)}">
<xsl:apply-templates select="*|@*"/>
</OrderLines>
</xsl:template>
然后,将这些规则组合到样式表中,您可以使用与您已经完成的代码非常相似的代码从Java代码运行转换:
TransformerFactory tf = TransformerFactory.newInstance();
Templates t = tf.newTemplates(new StreamSource(new File("stylesheet.xsl")));
StringWriter writer = new StringWriter();
t.newTransformer().transform(
new StreamSource(new File("input.xml")),
new StreamResult(writer));
答案 1 :(得分:-3)
如果您所做的只是更改标记和属性的文本,而不是XML文件本身的结构,那么您可以简单地将其作为文本文件读取,并使用Regex将一个字符串的所有实例与另一个字符串交换。以下代码可以帮助您入门:
//read in the file one line at a time
BufferedReader in = new BufferedReader(new FileReader("input.xml"));
FileOutputStream fos = new FileOutputStream("output.xml");
try {
StringBuilder sb = new StringBuilder();
String l = "";
String nl = System.getProperty("line.separator");
//get next line, assign it to l, and make sure it exists. The line after the last in the file will always be null
while((l = in.readLine()) != null) {
//replace the desired substrings of each line
if(l.contains("PrimeLineNo")) {
l = l.replaceAll("PrimeLineNo", "LineNumber");
}
if(l.contains("SubLineNo")) {
l = l.replaceAll("SubLineNo", "SubLineNumber");
}
//add the line to a string buffer to be written to file later
sb.append(l + nl);
}
//after we have gone through the entire input file, write it to the output file
fos.write(sb.toString().getBytes());
}
catch(Exception e) {
System.out.println("An unknown error occurred");
}
finally {
//ensure the streams will always close
in.close();
fos.close();
}