2012-04-03 1 views
0

Dies ist meine XML-DateiJAXB: Überprüfen Sie bitte die Struktur für die XML-java in JAXB

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

Diese Klasse mein Parser ist

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()); 

    } 
} 

Dies ist 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; 
} 


} 

Dies ist gegeben.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; 
    } 
} 
Diese

ist 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; 
    } 

} 

Dies ist Bag.java

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; 
    } 

} 

Als ich lief, dass ich

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) 

Antwort

1

Sie müssen die @XmlAttribute verwenden bin immer für die IDC Variable der Bag Klasse. Sobald Sie diese Änderung vorgenommen haben, funktioniert das XML, auf das Sie oben verwiesen haben.

Wie für Ihre aktuellen Code wird die XML erwartet aussehen:

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

Sie leicht erkennen können, welche Art von XML erwarten Ihre Klassen von allen Feldern Attribut bevölkern und dann das Objekt ein Marshalling Datei.

aktualisieren

Sie sollten immer Ihre Attribute als private deklarieren, wenn Sie richtig benannt Getter und Setter haben werden. Andernfalls wirft JAXB den Fehler .

Wenn er erklärt, dass ein Klassenattribut ist ein @XmlAttribute, sollten Sie die Anmerkung auf dem Getter setzen auch so, dass JAXB Sie sowohl eine @XmlAttribute und @XmlElement mit demselben Namen nicht denken.

public class Bag { 
    private String IDC ; 

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

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

Vielen Dank, also wenn ich die @XmlAttribute für die IDC verwenden wird der Code für die dieser XML arbeiten Pawan

+0

Ja. Ich habe die Antwort aktualisiert, um das klarzustellen. –

+0

Hallo, Danke für die schnelle Antwort, als ich das lief, bekomme ich 2 Zählungen von IllegalAnnotationExceptions, ich habe meine Frage aktualisiert, bitte sagen Sie mir, was falsch ist – Pawan