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