2012-05-12 15 views
5

मैं एक एक्सएमएल विशेषताओं के लिए नामस्थान है कि unmarshall की जरूरत के लिए अशक्त देता है, उदाहरण केJAXB एक namespace के साथ विशेषताओं

<license license-type="open-access" xlink:href="http://creativecommons.org/licenses/by/2.0/uk/"><license-p> 

इस विशेषता के रूप में

@XmlAttribute(namespace = "http://www.w3.org/TR/xlink/") 
@XmlSchemaType(name = "anySimpleType") 
protected String href; 

परिभाषित किया गया है के लिए लेकिन जब मैं करने की कोशिश href को पुनः प्राप्त करें, यह शून्य है। सही मूल्य प्राप्त करने के लिए मुझे जैक्सबी कोड में क्या जोड़ना/संशोधित करना चाहिए? मैंने पहले से ही नामस्थान से बचने की कोशिश की है, लेकिन यह काम नहीं किया, अभी भी शून्य। मैंने @XmlAttribute(namespace = "http://www.w3.org/TR/xlink/", name = "href") के साथ भी कोशिश की लेकिन यह या तो काम नहीं किया।

एक्सएमएल फ़ाइल के शीर्ष है:

<DOCTYPE article 
    PUBLIC "-//NLM//DTD v3.0 20080202//EN" "archive.dtd"> 
<article xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" article-type="article"> 
+0

क्या xml फ़ाइल के शीर्ष की तरह लग रहा है? –

उत्तर

3

नीचे कैसे @XmlAttribute एनोटेशन पर namespace संपत्ति निर्दिष्ट करने के लिए का एक उदाहरण है।

input.xml

<article xmlns:xlink="http://www.w3.org/1999/xlink"> 
    <license xlink:href="http://creativecommons.org/licenses/by/2.0/uk/"/> 
</article> 

लाइसेंस

package forum10566766; 

import javax.xml.bind.annotation.XmlAttribute; 

public class License { 

    private String href; 

    @XmlAttribute(namespace="http://www.w3.org/1999/xlink") 
    public String getHref() { 
     return href; 
    } 

    public void setHref(String href) { 
     this.href = href; 
    } 

} 

अनुच्छेद

package forum10566766; 

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class Article { 

    private License license; 

    public License getLicense() { 
     return license; 
    } 

    public void setLicense(License license) { 
     this.license = license; 
    } 

} 

डेमो

package forum10566766; 

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

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

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum10566766/input.xml"); 
     Article article = (Article) unmarshaller.unmarshal(xml); 

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

} 

आउटपुट

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<article xmlns:ns1="http://www.w3.org/1999/xlink"> 
    <license ns1:href="http://creativecommons.org/licenses/by/2.0/uk/"/> 
</article> 

नामस्थान उपसर्ग नियंत्रण करना चाहते हैं?

आप नाम स्थान जब दस्तावेज़ एक्सएमएल के लिए मार्शल का इस्तेमाल किया जाता निम्न आलेख की जाँच उपसर्गों को नियंत्रित करना चाहते हैं:

+1

समस्या मूल कोड में नामस्थान के साथ थी, यह कोड में 'http://www.w3.org/TR/xlink/' था लेकिन 'http://www.w3.org/1999/xlink' में एक्सएमएल @ ब्लेज़-डोफन का जवाब मुझे यह समझने में मदद करता है कि समस्या क्या थी। धन्यवाद – ljgc