问题描述
给定初始XML(BPEL)文件:
Given the intitial XML (BPEL) file:
<?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中的一些数据。
该函数如下:
I have written a function that uses JAXB in order to modify some data inside the XML.The function is as follows:
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是:
The resulting XML after the JAXB-related editing is:
<!-- 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。
Unfortunately the resulting XML, is not compliant to our application, as our XML parser crashes when is parsing the new XML.
所以:
- 如何删除命名空间
ns0
,在生成的XML中? - 如何保留初始XML文件中的相同标头(
xml:xsd
缺少)?
- How do I remove the namespace
ns0
, in the resulting XML ? - How to I preserve the same header from the initial XML File (the
xml:xsd
is missing)?
谢谢!
推荐答案
如果你使用实施你可以执行以下操作:
If you use the MOXy JAXB implementation you can do the following:
您的域对象:
package example;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Process {
}
使用该包注释@XmlSchema
Use that package annotation @XmlSchema
@javax.xml.bind.annotation.XmlSchema(
namespace = "http://www.example.org",
xmlns = {
@javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI = "http://www.w3.org/2001/XMLSchema"),
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package example;
要使用MOXy JAXB,您需要在模型类中添加一个jaxb.properties文件,其中包含以下内容:条目:
To use MOXy JAXB you need to add a jaxb.properties file in with your model classes with the following entry:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
这将生成XML:
<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://www.example.org" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
这篇关于JAXB编组问题 - 可能与命名空间有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!