2010-08-03 52 views
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缺失)?

謝謝!

+0

我只注意到在你的代碼每次調用「editActition」創建的JAXBContext的實例。 JAXBContext是線程安全的,因此您可以創建一次,然後重新使用該實例。 – 2010-08-03 18:00:04

+0

@Blaise Doughan謝謝布萊斯。這個功能是原型的一部分。我們正在評估不同的圖書館,我們來到JAXB。這不是一個「真正的」項目。但我一定會考慮你的觀察。 – 2010-08-03 18:59:06

+0

我想你會發現JAXB是最強大的XML綁定庫。我是JAXB專家組的成員,領導MOXy JAXB實施。如果您在評估過程中遇到JAXB問題,請隨時在此發佈問題或與我聯繫[email protected]。 – 2010-08-03 19:17:29

回答

1

如果使用MOXy JAXB實現,你可以做到以下幾點:

你的域對象:

package example; 

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class Process { 

} 

使用該程序包註釋@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; 

要使用莫西JAXB你需要使用以下條目在模型類中添加jaxb.properties文件:

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"/>