2012-04-03 11 views
0

におけるJavaのXMLのための構造を確認してくださいJAXB:これは私のXMLファイルであるJAXB

<BADFM> 
<Given> 
<Ord> 
<Bag IDC="DM" /> 
</Ord> 
</Given> 
</BADFM> 

これはこれはMyMessage

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

@XmlRootElement(name="BADFM") 
public class MyMessage 
{ 
@XmlElement(name="Given") 
private Given given; 

public Given getGiven() { 
    return given; 
} 

public void setGiven(Given given) { 
    this.given = given; 
} 


} 

ある

import java.io.File; 

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

public class Test { 
    public static void main(String args[]) throws Exception { 

     File file = new File("D:\\BADML.xml"); 
     JAXBContext jaxbContext = JAXBContext 
       .newInstance(MyMessage.class); 
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
     MyMessage authentifyResult = (MyMessage) jaxbUnmarshaller 
       .unmarshal(file); 
     System.out.println(authentifyResult.getGiven().getOrd().getBag().getIDC()); 

    } 
} 

私ParserクラスでありますこれはGivenです.java

import javax.xml.bind.annotation.XmlElement; 

public class Given { 
    private Ord ord; 
    @XmlElement(name = "Ord") 
    public Ord getOrd() { 
     return ord; 
    } 
    public void setOrd(Ord ord) { 
     this.ord = ord; 
    } 
} 

これは私があなたのために@XmlAttributeを使用する必要が

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions 
Class has two properties of the same name "IDC" 
    this problem is related to the following location: 
     at public java.lang.String Bag.getIDC() 
     at Bag 
     at public Bag Ord.getBag() 
     at Ord 
     at public Ord Given.getOrd() 
     at Given 
     at public Given MyMessage.getGiven() 
     at MyMessage 
    this problem is related to the following location: 
     at private java.lang.String Bag.IDC 
     at Bag 
     at public Bag Ord.getBag() 
     at Ord 
     at public Ord Given.getOrd() 
     at Given 
     at public Given MyMessage.getGiven() 
     at MyMessage 

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source) 
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) 
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source) 
    at javax.xml.bind.ContextFinder.find(Unknown Source) 
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
    at Test.main(Test.java:11) 

答えて

1

を取得していますことを実行したときこれは

import javax.xml.bind.annotation.XmlAttribute; 

public class Bag { 
     @XmlAttribute(name="IDC") 
    private String IDC ; 

    public String getIDC() { 
     return IDC; 
    } 
    @XmlAttribute(name="IDC") 
    public void setIDC(String IDC) { 
     IDC = IDC; 
    } 

} 

Bag.javaあるOrd.java

import javax.xml.bind.annotation.XmlElement; 

public class Ord { 

    private Bag bag; 
    @XmlElement(name="Bag") 
    public Bag getBag() { 
     return bag; 
    } 

    public void setBag(Bag bag) { 
     this.bag = bag; 
    } 

} 

ですBagクラスのIDC変数。変更を加えると、一番上で参照したXMLが機能します。あなたの現在のコードについては、それが見えるようにXMLを期待している

<Bag> 
    <IDC>DM</IDC> 
</Bag> 

あなたが簡単にあなたのクラスは、すべての属性フィールドを取り込むことで期待して、その後にオブジェクトをマーシャリングされているXMLの種類を見ることができますファイル。

更新

あなたが適切という名前のゲッターとセッターを持ってしようとしている場合は、いつでもプライベートとしてあなたの属性を宣言する必要があります。そうしないと、JAXBはエラーClass has two properties of the same nameをスローします。 JAXBは、同じ名前の両方@XmlAttribute@XmlElementたいと思わないように

クラス属性が@XmlAttributeであることを宣言し、あなたはまた、ゲッターに注釈をつけなければなりません。

public class Bag { 
    private String IDC ; 

    @XmlAttribute(name="IDC") 
    public String getIDC() { 
     return IDC; 
    } 

    public void setIDC(String IDC) { 
     this.IDC = IDC; 
    } 
} 
+0

どうもありがとう、私はIDCのため@XmlAttributeを使用する場合のコードは、このXML <バッグIDC = "DM" /> のために動作しますしますので、 – Pawan

+0

はい。私はそれを明確にするために答えを更新しました。 –

+0

こんにちは、速やかな応答ありがとう、私はそれを実行したとき、私はIllegalAnnotationExceptionsの2つのカウントを取得している、私は質問を更新した何か間違って教えてください – Pawan