Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA Transformer向XML头添加空格和单引号,而不编码生成的XML文件?_Java_Xml_Encoding_Jaxb_Xml Signature - Fatal编程技术网

JAVA Transformer向XML头添加空格和单引号,而不编码生成的XML文件?

JAVA Transformer向XML头添加空格和单引号,而不编码生成的XML文件?,java,xml,encoding,jaxb,xml-signature,Java,Xml,Encoding,Jaxb,Xml Signature,我有以下JAXB marshaller,它正在使用DOM结果,以便稍后添加XML数字签名: JAXBContext jaxbContext = JAXBContext.newInstance(DataPDU.class); DataPDU myDataPDU = new DataPDU(); myDataPDU.setRevision("2.0.6"); // marshall the file Marshaller marshaller = jaxbContext.createMarshal

我有以下JAXB marshaller,它正在使用DOM结果,以便稍后添加XML数字签名:

JAXBContext jaxbContext = JAXBContext.newInstance(DataPDU.class);
DataPDU myDataPDU = new DataPDU();
myDataPDU.setRevision("2.0.6");

// marshall the file
Marshaller marshaller = jaxbContext.createMarshaller();

DOMResult domResult = new DOMResult();
marshaller.marshal(myDataPDU, domResult);

// get the document list
Document document = (Document) domResult.getNode();

// create the output file
File kadSignatureOutputFile = new File("pptExampleTest.xml");

// create the file output stream to be used in the XSL transformation
OutputStream os = new FileOutputStream(kadSignatureOutputFile);

// create the factory and the transformer
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();

// set the document to print the out with indent and UTF-8 encoded
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

// transform the DOM source to a stream source (i.e the file we are in)
trans.transform(new DOMSource(document), new StreamResult(os));
我这里有一些问题,输出文件没有用UTF-8编码,即使编码设置为UTF-8,但在记事本++中它没有被标记为已编码,软件提供了将文件转换为UTF-8的选项

如何确保文件在转换时正确编码 通过JavaAPI

另一件事,xml头属性值被单引号引用,并且在属性名称和值之间有空格,我不确定稍后在对文档签名时是否会考虑这个标记(xml头),但如果它这样做,这将是一个大问题

如何获取带有双引号且无空格的标准XML标头 使用变压器

结果:

<?xml version = '1.0' encoding = 'UTF-8'?>

期望:

<?xml version="1.0" encoding="UTF-8"?>


对于编码问题,可能是
trans.transform(新的DOMSource(文档)、新的StreamResult(新的OutputStreamWriter(新的FileOutputStream(“pptExampleTest.xml”)、“utf-8”)通过设置输出编码来提供帮助。对于另一个问题:您使用的是什么java版本?没有真正的帮助,我使用的是JDK7。
TransformerFactory.newInstance()
返回的对象的具体类名是什么?此方法有多个实现,我们需要知道您选择的是哪一个。请注意,您看到的XML声明是完全合法的,尽管这有点不寻常,而且查找XML声明的低质量软件可能无法识别此表单中的XML声明。谢谢。那不是我熟悉的。它似乎是OracleXDK的一部分(并不是最优秀的)。您可以通过显式加载不同的实现来解决此问题,例如将Saxon HE放在类路径上,并执行
TransformerFactory tf=new net.sf.Saxon.TransformerFactoryImpl()