2011-07-20 14 views
6

में कोई विशेषता जोड़ना मैं कुछ JAXB पार्सिंग के साथ संघर्ष कर रहा हूं और कुछ मार्गदर्शन की आवश्यकता है।किसी JAXB तत्व

अनिवार्य रूप से, मैं अपने क्लास वेरिएबल्स में विशेषताओं को जोड़ने की कोशिश कर रहा हूं जिन्हें मैंने पहले ही @XmlElement का उपयोग करके तत्वों के रूप में घोषित कर दिया है। अब तक, @XmlAttribute का उपयोग करने का कोई भी प्रयास वर्ग स्तर पर विशेषता सेट करता है।

<DataClass newAttribute="test"> 
    <myElement>I wish this element had an attribute</myElement> 
    <anotherElement>I wish this element had an attribute too</anotherElement> 
</DataClass> 

मैं यह करने के लिए करना चाहते हैं:

<DataClass> 
    <myElement thisAtt="this is what I'm talking about">This is better</myElement> 
    <anotherElement thisAtt="a different attribute here">So is this</anotherElement> 
</DataClass> 

मैंने देखा है अन्य पदों का उपयोग कर एक भी तत्व को एक विशेषता जोड़

यह क्या मैं "वर्तमान में हो रही हूँ है @XmlValue, लेकिन जब आपके पास तत्व होते हैं तो यह काम नहीं करता है, और कई तत्वों पर काम नहीं करेगा।

क्या किसी के पास यह विचार किया जा सकता है कि यह कैसे पूरा किया जा सकता है?

धन्यवाद! जेसन

+0

आप w इसलिए प्रासंगिक जावा कोड पोस्ट करना होगा ई देख सकता है कि वास्तव में क्या चल रहा है। –

उत्तर

3

यह है कि XML पैदा करेगा:

public class JaxbAttributes { 
    public static void main(String[] args) throws Exception { 
     Marshaller marshaller = 
      JAXBContext.newInstance(DataClass.class).createMarshaller(); 
     StringWriter stringWriter = new StringWriter(); 
     DataClass dataClass = new DataClass(
       new Foo("this is what I'm talking about", "This is better"), 
       new Foo("a different attribute here", "So is this")); 
     marshaller.marshal(dataClass, stringWriter); 
     System.out.println(stringWriter); 
    } 

    @XmlRootElement(name = "DataClass") 
    @XmlType(propOrder = {"myElement", "anotherElement"}) 
    static class DataClass { 
     private Foo myElement; 
     private Foo anotherElement; 

     DataClass() {} 
     public DataClass(Foo myElement, Foo anotherElement) { 
      this.myElement = myElement; 
      this.anotherElement = anotherElement; 
     } 

     public Foo getMyElement() { return myElement; } 
     public void setMyElement(Foo myElement) { this.myElement = myElement; } 
     public Foo getAnotherElement() { return anotherElement; } 
     public void setAnotherElement(Foo anotherElement) { this.anotherElement = anotherElement; } 
    } 

    static class Foo { 
     private String thisAtt; 
     private String value; 

     Foo() {} 
     public Foo(String thisAtt, String value) { 
      this.thisAtt = thisAtt; 
      this.value = value; 
     } 

     @XmlAttribute 
     public String getThisAtt() { return thisAtt; } 
     public void setThisAtt(String thisAtt) { this.thisAtt = thisAtt; } 
     @XmlValue 
     public String getValue() { return value; } 
     public void setValue(String value) { this.value = value; } 
    } 
} 
+0

मेरा फू आपके कर्ज में है। आपका बहुत बहुत धन्यवाद। मैंने इस तरह इसका उपयोग करने के बारे में सोचा नहीं था, इसका मूल ओओ लेकिन किसी कारण से मैंने इसे नहीं देखा। एक बार फिर, बहुत बहुत धन्यवाद। – JasonH

2

नोट: मैं EclipseLink JAXB (MOXy) नेतृत्व कर रहा हूँ, और JAXB 2.X के एक सदस्य (JSR-222) विशेषज्ञ समूह।

DataClass

@XmlPath एनोटेशन मानक JAXB एनोटेशन के साथ इस्तेमाल किया जा सकता:

वैकल्पिक रूप से आप उपयोग के इस मामले को संभालने के लिए MOXY में @XmlPath विस्तार इस्तेमाल कर सकते हैं

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

import org.eclipse.persistence.oxm.annotations.XmlPath; 

@XmlRootElement(name="DataClass") 
@XmlType(propOrder={"myElement", "anotherElement"}) 
public class DataClass { 

    private String myElement; 
    private String myElementThisAtt; 
    private String anotherElement; 
    private String anotherElementThisAtt; 

    public String getMyElement() { 
     return myElement; 
    } 

    public void setMyElement(String myElement) { 
     this.myElement = myElement; 
    } 

    @XmlPath("myElement/@thisAtt") 
    public String getMyElementThisAtt() { 
     return myElementThisAtt; 
    } 

    public void setMyElementThisAtt(String myElementThisAtt) { 
     this.myElementThisAtt = myElementThisAtt; 
    } 

    public String getAnotherElement() { 
     return anotherElement; 
    } 

    public void setAnotherElement(String anotherElement) { 
     this.anotherElement = anotherElement; 
    } 

    @XmlPath("anotherElement/@thisAtt") 
    public String getAnotherElementThisAtt() { 
     return anotherElementThisAtt; 
    } 

    public void setAnotherElementThisAtt(String anotherElementThisAtt) { 
     this.anotherElementThisAtt = anotherElementThisAtt; 
    } 

} 

डेमो

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(DataClass.class); 

     DataClass dataClass = new DataClass(); 
     dataClass.setMyElement("This is better"); 
     dataClass.setMyElementThisAtt("this is what I'm talking about"); 
     dataClass.setAnotherElement("So is this"); 
     dataClass.setAnotherElementThisAtt("a different attribute here"); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(dataClass, System.out); 
    } 
} 

आउटपुट

<?xml version="1.0" encoding="UTF-8"?> 
<DataClass> 
    <myElement thisAtt="this is what I'm talking about">This is better</myElement> 
    <anotherElement thisAtt="a different attribute here">So is this</anotherElement> 
</DataClass> 

अधिक जानकारी