我只是将XML文件加载到我的代码中,然后使用Transformer再次打印出来。这改变了结果。它应该与原始版本完全相同。我不能为我的生活找出问题所在。
加载XML文档的代码:
xmlFile = new File(xmlFilePath);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
cxmlDocument = db.parse(xmlFile);
cxmlElement = cxmlDocument.getDocumentElement();
将其打印到字符串的代码:
javax.xml.transform.Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(cxmlDocument);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
return xmlString;
保存文档的代码:
FileUtils.writeStringToFile(new File("result.xml"), print());
是的,我正在做错误处理等等。我刚刚没有在这里展示它。
原始XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.2.009/cXML.dtd">
<cXML payloadID="538fc224eb006" timestamp="06/04/2014 08:04:36 pm" signatureVersion="1.0" version="1.2.015" xml:lang="en-US">
<Header>
<From>
<Credential domain="NetworkID">
<Identity></Identity>
</Credential>
</From>
<To>
<Credential domain="NetworkId">
<Identity></Identity>
</Credential>
</To>
<Sender>
<Credential domain="NetworkID">
<Identity></Identity>
<SharedSecret />
</Credential>
<UserAgent></UserAgent>
</Sender>
</Header>
<Message Id="cXMLData" deploymentMode="production">
<PunchOutOrderMessage>
<PunchOutOrderMessageHeader operationAllowed="create" quoteStatus="final">
<Total>
<Money currency="USD">17.96</Money>
</Total>
</PunchOutOrderMessageHeader>
<ItemIn quantity="1">
<ItemID>
<SupplierPartID>2889</SupplierPartID>
<SupplierPartAuxiliaryID>2917</SupplierPartAuxiliaryID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD">16.59</Money>
</UnitPrice>
<Description xml:lang="en">Men's Port Authority Long Sleeve Easy Care Shirt</Description>
<UnitOfMeasure>EA</UnitOfMeasure>
</ItemDetail>
</ItemIn>
</PunchOutOrderMessage>
</Message>
</cXML>
结果:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><cXML payloadID="538e338b3db95" signatureVersion="1.0" timestamp="1970-01-01T12:00:00" version="1.2.015" xml:lang="en-US">
<Header>
<From>
<Credential domain="NetworkID">
<Identity></Identity>
</Credential>
</From>
<To>
<Credential domain="NetworkId">
<Identity></Identity>
</Credential>
</To>
<Sender>
<Credential domain="NetworkID">
<Identity></Identity>
<SharedSecret/>
</Credential>
<UserAgent></UserAgent>
</Sender>
</Header>
<Message Id="cXMLData" deploymentMode="production">
<PunchOutOrderMessage>
<PunchOutOrderMessageHeader operationAllowed="create" quoteStatus="final">
<Total>
<Money currency="USD">17.96</Money>
</Total>
</PunchOutOrderMessageHeader>
<ItemIn quantity="1">
<ItemID>
<SupplierPartID>2889</SupplierPartID>
<SupplierPartAuxiliaryID>2918</SupplierPartAuxiliaryID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD">16.59</Money>
</UnitPrice>
<Description xml:lang="en">Men's Port Authority Long Sleeve Easy Care Shirt</Description>
<UnitOfMeasure>EA</UnitOfMeasure>
</ItemDetail>
</ItemIn>
</PunchOutOrderMessage>
</Message>
</cXML>
正如你可以看到这3行:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.2.009/cXML.dtd">
<cXML payloadID="538fc224eb006" timestamp="06/04/2014 08:04:36 pm" signatureVersion="1.0" version="1.2.015" xml:lang="en-US">
已更改为:
<?xml version="1.0" encoding="UTF-8"?>
<cXML payloadID="538e338b3db95" signatureVersion="1.0" timestamp="1970-01-01T12:00:00" version="1.2.015" xml:lang="en-US">
时间戳不同。有效载荷值也是如此。 DOCTYPE线完全消失了。此外,属性的顺序也不同。
另外,完全随机,我能告诉的唯一另一件事是价值:
<SupplierPartAuxiliaryID>2917</SupplierPartAuxiliaryID>
为:
<SupplierPartAuxiliaryID>2918</SupplierPartAuxiliaryID>
任何人都可以帮我理解这个吗?我做错了什么?
答案 0 :(得分:0)
您提到的一些更改是可预测的。转换不会保留<!DOCTYPE>
声明。事实上,它甚至没有阅读它。但是可以使用变换器属性重新生成它:
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://xml.cXML.org/schemas/cXML/1.2.009/cXML.dtd");
根据XML规范,属性顺序无关紧要,因此解析器不需要保留它。
如果您只是阅读文件并将其打印出来,则payload
,timestamp
和<SupplierPartAuxiliaryID>
中的更改不会发生。如果它是生成的文件,请检查您是否未与过期版本进行比较。时间戳和有效负载可能是生成的值。当然,其他一些过程正在改变这些数据。如果您按照描述使用它们,解析器和转换将不会更改数据。