正在生成的xml是
<Interface xsi:noNamespaceSchemaLocation="bla-bla.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">但是,由于某种原因,正在解析此xml的应用程序并没有解析它,因为它们需要这种格式的xml
<Interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bla-bla.xsd">更改目标应用程序不是一个选项:(
发布于 2013-02-08 01:17:07
以下利用JAXB和StAX的方法似乎可以为您提供所需的输出,但由于属性的顺序并不重要,因此不能保证它始终有效。
import javax.xml.bind.*;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Interface.class);
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "bla-bla.xsd");
marshaller.marshal(new Interface(), xsw);
}
}输出
<?xml version="1.0"?><Interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bla-bla.xsd"></Interface>https://stackoverflow.com/questions/14748593
复制相似问题