1
鉴于初使XML(BPEL)文件:JAXB编组问题 - 可能的命名空间相关
<?xml version="1.0" encoding="UTF-8"?>
<process
name="TestSVG2"
xmlns="http://www.example.org"
targetNamespace="http://www.example.org"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<sequence>
<receive name="Receive1" createInstance="yes"/>
<assign name="Assign1"/>
<invoke name="Invoke1"/>
<assign name="Assign2"/>
<reply name="Reply1"/>
</sequence>
</process>
我写了使用JAXB为了修改XML里面的一些数据的功能。 的功能如下:
public void editAction(String name, String newName) {
Process proc;
StringWriter sw = new StringWriter();
JAXBContext jaxbContext = null;
Unmarshaller unMarsh = null;
Object obj = new Object();
try {
/* XML TO JAVA OBJECT */
jaxbContext = JAXBContext.newInstance("org.example");
unMarsh = jaxbContext.createUnmarshaller();
obj = unMarsh.unmarshal(new File(path + "/resources/" + BPELFilename));
proc = (Process) obj;
Process.Sequence sequence = proc.getSequence();
/* Determine which element needs to be edited */
/* Do some editing , code wasn't included */
/* OBJ Back to XML */
Marshaller marsh = jaxbContext.createMarshaller();
marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//marsh.setProperty("com.sun.xml.bind.namespacePrefixMapper", new CustomPrefixMapper());
marsh.marshal(obj, new File(path + "/resources/" + BPELFilename));
} catch (JAXBException e) {
/* Be afraid */
e.printStackTrace();
}
}
的JAXB相关编辑后生成的XML是:
<!-- After -->
<?xml version="1.0" encoding="UTF-8"?>
<ns0:process
name="TestSVG2"
targetNamespace="http://www.example.org"
xmlns:ns0="http://www.example.org">
<ns0:sequence>
<ns0:receive name="newName" createInstance="yes"/>
<ns0:assign name="Assign1"/>
<ns0:assign name="Assign2"/>
<ns0:invoke name="Invoke1"/>
<ns0:reply name="Reply1"/>
</ns0:sequence>
</ns0:process>
不幸的是,生成的XML,是不符合我们的应用程序,为我们的XML解析器崩溃时解析新的XML。
所以:
- 如何删除命名空间
ns0
,在生成的XML? - 如何从最初的XML文件保留相同的标题(
xml:xsd
缺失)?
谢谢!
我只注意到在你的代码每次调用“editActition”创建的JAXBContext的实例。 JAXBContext是线程安全的,因此您可以创建一次,然后重新使用该实例。 – 2010-08-03 18:00:04
@Blaise Doughan谢谢布莱斯。这个功能是原型的一部分。我们正在评估不同的图书馆,我们来到JAXB。这不是一个“真正的”项目。但我一定会考虑你的观察。 – 2010-08-03 18:59:06
我想你会发现JAXB是最强大的XML绑定库。我是JAXB专家组的成员,领导MOXy JAXB实施。如果您在评估过程中遇到JAXB问题,请随时在此发布问题或与我联系[email protected]。 – 2010-08-03 19:17:29