2010-07-08 11 views
5

कुछ सुंदर गंदा एक्सएमएल है जो मैं जैक्सबी का उपयोग कर जावा ऑब्जेक्ट को अनारश करना चाहता हूं। इसमें से अधिकांश अब तक काफी सरल लग रहा था की है - लेकिन मैं थोड़े इस पर अटक कर रहा हूँ:जेएक्सबी - इस एक्सएमएल को कैसे उतारना है?

  <assets> 
       <asset type="fixed">74,414</asset> 
       <asset type="current">1,022,069</asset> 
       <asset type="other">0</asset> 
       <total type="assets">1,096,483</total> 
      </assets> 

इस DTD

<!ELEMENT assets (asset|total)*> <!ELEMENT asset (#PCDATA)> <!ATTLIST asset type CDATA #REQUIRED> <!ELEMENT total (#PCDATA)> <!ATTLIST total type CDATA #REQUIRED>

कोई भी विचार के प्रासंगिक हिस्सा है? या क्या मुझे इसके लिए जेएक्सबी का उपयोग करने की कोशिश करनी चाहिए?

धन्यवाद

+0

मुझे नहीं पता कि डीटीडी यहां कैसे प्रासंगिक है ... आप किस प्रकार की ऑब्जेक्ट संरचना को मानचित्र बनाना चाहते हैं? – skaffman

+0

मेरे पास अभी तक एक कक्षा नहीं है जिसे मैं अनचाहे कर रहा हूं, एक्सएमएल को पहले SAX हैंडलर द्वारा संभाला गया था। मुझे लगता है कि मैं संरचना को आजमाकर देखना चाहता हूं - जैसा कि मुझे इसके साथ करने की ज़रूरत है, इसे एक एचटीएमएल/पीडीएफ टेबल में प्रदर्शित करना है; इसकी संभावित दैनिक सामग्री, और बदतर है! यह एक्सएमएल का सिर्फ एक छोटा सा हिस्सा है। यहां दिए गए उत्तरों से ऐसा लगता है कि मुझे संग्रह का उपयोग करना होगा; मैं जेएक्सबी और एक्सएमएल की दुनिया में नया हूं, लेकिन उम्मीद है कि यहां दिए गए उत्तरों को मुझे कुछ विचार देना चाहिए यदि मैं इस मार्ग को जारी रखूं। –

उत्तर

5

एक्सएमएल और DTD को देखते हुए, मैं संरचना के XSD बनाया:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"> 
    <xs:element name="assets"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element maxOccurs="unbounded" ref="asset"/> 
     <xs:element maxOccurs="unbounded" ref="total"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="asset"> 
    <xs:complexType mixed="true"> 
     <xs:attribute name="type" use="required" type="xs:string"/> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="total"> 
    <xs:complexType mixed="true"> 
     <xs:attribute name="type" use="required" type="xs:string"/> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

उपयोग xjc XSD से JAXB बाध्यकारी एनोटेशन के साथ एनोटेट जावा वर्गों उत्पन्न करने के लिए। फिर जावा ऑब्जेक्ट को अनारशल करने के लिए unmarshaller का उपयोग करें।

संपादित

जनरेट किया गया जावा वर्गों:

import java.util.ArrayList; 
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; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlValue; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "asset", 
    "total" 
}) 
@XmlRootElement(name = "assets") 
public class Assets { 

    @XmlElement(required = true) 
    protected List<Asset> asset; 
    @XmlElement(required = true) 
    protected List<Total> total; 

    public List<Asset> getAsset() { 
     if (asset == null) { 
      asset = new ArrayList<Asset>(); 
     } 
     return this.asset; 
    } 

    public List<Total> getTotal() { 
     if (total == null) { 
      total = new ArrayList<Total>(); 
     } 
     return this.total; 
    } 

} 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
@XmlRootElement(name = "asset") 
public class Asset { 

    @XmlValue 
    protected String content; 
    @XmlAttribute(required = true) 
    protected String type; 

    public String getContent() { 
     return content; 
    } 

    public void setContent(String value) { 
     this.content = value; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String value) { 
     this.type = value; 
    } 

} 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
@XmlRootElement(name = "total") 
public class Total { 

    @XmlValue 
    protected String content; 
    @XmlAttribute(required = true) 
    protected String type; 

    public String getContent() { 
     return content; 
    } 

    public void setContent(String value) { 
     this.content = value; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String value) { 
     this.type = value; 
    } 

} 
1

आप एक वर्ग आप के लिए unmarshalling रहे है? ऐसा लगता है कि यह निम्नलिखित की आवश्यकता होगी लगता है:

/** AssetContainer */ 
@XmlRootElement(namespace = "project/schema") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class AssetContainer implements Unmarshallable { 
    private List<Asset> assetList; 
    private int totalAssets; 
} 

/** Asset */ 
@XmlType 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Asset implements Unmarshallable { 
    private AssetTypeEnum type; 
    private int count; 
} 

/** Unmarshallable */ 
public interface Unmarshallable { 
    // Marker interface 
} 

और फिर एक XmlTypeAdapter का उपयोग उचित वर्ग के लिए XML तत्वों को मैप करने के।

संबंधित मुद्दे