2012-12-04 7 views
5

JAXBを使用してxmlファイルをアンマーシャルしようとしています。 @ XmlElement(name = "name"、namespace = "http://www.test.com")などの@XmlElementを正しい名前と名前空間で使用しているときにアンマーシャリングが機能します。xmlとJAXBのアンマーシャリング - XmlTypeとプロポーザルのネームスペース

XmlTypeを一緒に使用するとpropOrderを使用すると、残念ながらそれ以上はありません(例:@XmlType(namespace = "http://www.test.com"、name = ""、propOrder = {"name"、 "description"})))。

XMLファイル(のtest.xml)の含有量:JAXBExample.javaの含有量が

<Operation xmlns="http://www.test.com"> 
    <Parameter> 
    <name>Param1</name> 
    <description>Description of Parameter1</description> 
    </Parameter> 
    <Parameter> 
    <name>Param2</name> 
    <description>Description of Parameter2</description> 
    </Parameter> 
</Operation> 

package stackoverflow.problem.jaxb.ns; 

import java.io.FileNotFoundException; 
import java.io.FileReader; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Unmarshaller; 

public class JAXBExample { 

    public static void main(String[] args) throws JAXBException, FileNotFoundException { 
    String xmlFilename = "test.xml"; 

    JAXBContext context = JAXBContext.newInstance(Operation.class); 

    System.out.println("Output from our XML File: "); 
    Unmarshaller um = context.createUnmarshaller(); 
    Operation op = (Operation) um.unmarshal(new FileReader(xmlFilename)); 

    System.out.println("Operation-Content: " + op); 
    } 

} 

パッケージstackoverflow.problemの内容。 jaxb.ns;

import java.util.List; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 


@XmlRootElement(name="Operation", namespace="http://www.test.com") 
@XmlAccessorType(XmlAccessType.FIELD) 

public class Operation { 

    @XmlElement(name = "Parameter", namespace="http://www.test.com") 
    List<Parameter> parameterList; 

    @Override 
    public String toString(){ 
    String retVal = ""; 
    for(Parameter currentParameter: parameterList){ 
     retVal += currentParameter.toString() + "\n"; 
    } 
    return retVal; 
    } 
} 

そしてParameter.javaの内容です:

package stackoverflow.problem.jaxb.ns; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 

@XmlType(namespace="http://www.test.com", name = "", propOrder = {"name", "description"}) 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Parameter { 

    //@XmlElement(name = "name", namespace="http://www.test.com") 
    String name; 

    //@XmlElement(name = "description", namespace="http://www.test.com") 
    String description; 

    @Override 
    public String toString(){ 
    return this.name + "\t" + this.description; 
    } 
} 

私は最後のコードブロック(Parameter.java)で2 @XmlElementの行のコメントを解除した場合、非整列化が正常に動作します。これらの2行が含まれていない場合、Parameterオブジェクトの両方のフィールドはnullです。 XmlTypeでpropOrderを使用するときに名前空間を宣言する別の方法はありますか?それとも、私は何か間違ったことをしましたか?

答えて

5

名前空間も、package-info.javaという名前のファイルには、パッケージごとにあなたはすべてのクラスやフィールドでそれを繰り返す必要はありません。このように定義することができます:

@javax.xml.bind.annotation.XmlSchema (
    namespace = "http://www.test.com", 
    elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED 
) 
package stackoverflow.problem.jaxb.ns; 
+2

1 - JAXBの詳細についてはとhttp://blog.bdoughan.com/2010/08/jaxb-namespaces.html –

+1

これを指摘すると、コードをすべてのフィールドに入れる必要がなくなり、コードが煩雑にならないだけでなく、 propOrderで名前空間を使用する問題を解決します。 – Exocom

関連する問題