2016-09-29 16 views
8
đoạn

XML:JAXB Unmarshals Nested Yếu tố để Null

<datasource formatted-name="blah" inline="blah"> 
    <repository-location derived-from="blah" id="blah" path="blah" revision="blah" site="blah"/> 
</datasource> 

Tôi cố gắng để unmarshal tất cả mọi thứ dưới một lớp (DataSource) với các lớp tĩnh lồng nhau. Đây là lớp DataSource của tôi:

@XmlRootElement(name = "datasource") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class DataSource { 

    @XmlAttribute(name = "formatted-name") 
    protected String formattedName; 
    @XmlAttribute(name = "inline") 
    protected String inline; 
    @XmlElement(name = "repository-location") 
    protected RepositoryLocation repositoryLocation; 

    // public getters and setters for fields above 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class RepositoryLocation { 

     @XmlAttribute(name = "derived-from") 
     protected String derivedFrom; 
     @XmlAttribute(name = "id") 
     protected String id; 
     @XmlAttribute(name = "path") 
     protected String path; 
     @XmlAttribute(name = "revision") 
     protected String revision; 
     @XmlAttribute(name = "site") 
     protected String site; 

     // public getters and setters for fields above 
    } 
} 

Unmarshaller:

JAXBContext jaxbContext = JAXBContext.newInstance(DataSource.class); 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 
StringReader reader = new StringReader(responseXML); 
dataSourceResponse = (DataSource) unmarshaller.unmarshal(reader); 

tôi có thể lĩnh vực DataSource đầu ra thành công "formattedName" và "nội tuyến", nhưng "repositoryLocation" là null. Ai đó có thể giúp đỡ được không?

+0

cần làm việc. Bạn có getters/setters cho 'repositoryLocation'? – ulab

+0

vâng, tôi có chúng. – toadead

+0

làm thế nào để bạn unmarshall? xin thêm mã. – ulab

Trả lời

2

Vấn đề giải quyết! Tôi nhận thấy thư viện java 1,7 của tôi là thiếu một số lọ. Vì vậy, tôi đã quyết định nâng cấp lên 1.8. Nó hoạt động như ma thuật!

3

JAXB có thể sắp xếp không có Getters/Setters và các trường thậm chí có thể là riêng tư. Với lớp DataSource trên, có một số được tạo ra toString phương pháp bổ sung vào cả DataSourceRepositoryLocation, các bản in sau ra tất cả các thuộc tính:

import javax.xml.bind.*; 
import javax.xml.bind.annotation.*; 
import java.io.StringReader; 

public class Jaxb { 

    public static void main(String[] args) throws JAXBException { 
     String xml = "<datasource formatted-name=\"blah\" inline=\"blah\">\n" + 
       " <repository-location derived-from=\"blah\" id=\"blah\"" + 
       " path=\"blah\" revision=\"blah\" site=\"blah\"/>\n" + 
       "</datasource>"; 

     JAXBContext context = JAXBContext.newInstance(DataSource.class); 
     Unmarshaller unmarshaller = context.createUnmarshaller(); 
     DataSource dataSource = (DataSource) unmarshaller.unmarshal(new StringReader(xml)); 
     System.out.println(dataSource); 
    } 

    @XmlRootElement(name = "datasource") 
    @XmlAccessorType(XmlAccessType.FIELD) 
    private class DataSource { 

     @XmlAttribute(name = "formatted-name") 
     private String formattedName; 
     @XmlAttribute(name = "inline") 
     private String inline; 
     @XmlElement(name = "repository-location") 
     private RepositoryLocation repositoryLocation; 

     @XmlAccessorType(XmlAccessType.FIELD) 
     private class RepositoryLocation { 

      @XmlAttribute(name = "derived-from") 
      private String derivedFrom; 
      @XmlAttribute(name = "id") 
      private String id; 
      @XmlAttribute(name = "path") 
      private String path; 
      @XmlAttribute(name = "revision") 
      private String revision; 
      @XmlAttribute(name = "site") 
      private String site; 

      @Override 
      public String toString() { 
       return "RepositoryLocation{" + 
         "derivedFrom='" + derivedFrom + '\'' + 
         ", id='" + id + '\'' + 
         ", path='" + path + '\'' + 
         ", revision='" + revision + '\'' + 
         ", site='" + site + '\'' + 
         '}'; 
      } 
     } 

     @Override 
     public String toString() { 
      return "DataSource{" + 
        "formattedName='" + formattedName + '\'' + 
        ", inline='" + inline + '\'' + 
        ", repositoryLocation=" + repositoryLocation + 
        '}'; 
     } 
    } 
} 
+0

Đây là những gì tôi nhận được: DataSource {formattedName = 'Cá nhân BI Mart (bản sao)', inline = 'true', phiên bản = '9.2', xmlBase = 'null', xmlnsUser = 'null', repositoryLocation = null} – toadead

2

Cố gắng với java 1,7 quá và nó hoạt động và trong xml.txt, xml được lưu

package testSingleJar.specificpackage; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.StringReader; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

/** 
* Hello world! 
* 
*/ 
public class App 
{ 
    public static void main(String[] args) throws Exception 
    { 
     JAXBContext jaxbContext = JAXBContext.newInstance(DataSource.class); 
     Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 
     String responseXML = ""; 
     //filename is filepath string 
     BufferedReader br = new BufferedReader(new FileReader(new File("D:\\xml.txt"))); 
     String line; 
     StringBuilder sb = new StringBuilder(); 

     while((line=br.readLine())!= null){ 
      sb.append(line.trim()); 
     } 

     responseXML=sb.toString(); 
     StringReader reader = new StringReader(responseXML); 
     DataSource dataSourceResponse = (DataSource) unmarshaller.unmarshal(reader); 
     System.out.println(dataSourceResponse.getRepositoryLocation().getDerivedFrom()); 
    } 
} 


@XmlRootElement(name = "datasource") 
@XmlAccessorType(XmlAccessType.FIELD) 
class DataSource { 

    @XmlAttribute(name = "formatted-name") 
    protected String formattedName; 
    @XmlAttribute(name = "inline") 
    protected String inline; 
    @XmlElement(name = "repository-location") 
    protected RepositoryLocation repositoryLocation; 

    // public getters and setters for fields above 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class RepositoryLocation { 

     @XmlAttribute(name = "derived-from") 
     protected String derivedFrom; 
     @XmlAttribute(name = "id") 
     protected String id; 
     @XmlAttribute(name = "path") 
     protected String path; 
     @XmlAttribute(name = "revision") 
     protected String revision; 
     @XmlAttribute(name = "site") 
     protected String site; 
     public String getDerivedFrom() { 
      return derivedFrom; 
     } 
     public void setDerivedFrom(String derivedFrom) { 
      this.derivedFrom = derivedFrom; 
     } 
     public String getId() { 
      return id; 
     } 
     public void setId(String id) { 
      this.id = id; 
     } 
     public String getPath() { 
      return path; 
     } 
     public void setPath(String path) { 
      this.path = path; 
     } 
     public String getRevision() { 
      return revision; 
     } 
     public void setRevision(String revision) { 
      this.revision = revision; 
     } 
     public String getSite() { 
      return site; 
     } 
     public void setSite(String site) { 
      this.site = site; 
     } 

    } 

    public String getFormattedName() { 
     return formattedName; 
    } 

    public void setFormattedName(String formattedName) { 
     this.formattedName = formattedName; 
    } 

    public String getInline() { 
     return inline; 
    } 

    public void setInline(String inline) { 
     this.inline = inline; 
    } 

    public RepositoryLocation getRepositoryLocation() { 
     return repositoryLocation; 
    } 

    public void setRepositoryLocation(RepositoryLocation repositoryLocation) { 
     this.repositoryLocation = repositoryLocation; 
    } 
} 

đầu ra: blah

Các vấn đề liên quan