使用命名空间的肥皂xml对象jaxb

时间:2015-06-25 12:16:00

标签: java xml soap jaxb

我是xml marshaling的新手 - 解组,我试图将以下带有命名空间的SOAP Xml转换为JAVA对象。

  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://example.com/test/V1" xmlns:v11="http://example.com/test//Core/V1/">
       <soapenv:Header/>
       <soapenv:Body>
         <question id="1">  
        <v1:answers>  
            <v11:answername>java is a programming language</v11:answername>  
            <v11:id>101</v11:id>  
            <v11:postedby>ravi</v11:postedby>  
        </v1:answers>  
        <v1:answers>  
            <v11:answername>java is a platform</v11:answername>  
            <v11:id>102</v11:id>  
            <v11:postedby>john</v11:postedby>  
        </v1:answers>  
        <v1:questionname>What is java?</v1:questionname>  
        <v1:questionnamedd>sdfsdfs</v1:questionnamedd>  
 </question>
  </soapenv:Body>
</soapenv:Envelope>

xml的相应类是

public class Answer {
    private int id;
    private String answername;
    private String postedby;

    public Answer() {
    }

    public Answer(int id, String answername, String postedby) {
        super();
        this.id = id;
        this.answername = answername;
        this.postedby = postedby;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAnswername() {
        return answername;
    }

    public void setAnswername(String answername) {
        this.answername = answername;
    }

    public String getPostedby() {
        return postedby;
    }

    public void setPostedby(String postedby) {
        this.postedby = postedby;
    }

}

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Question {
    private int id;
    private String questionname;
    private List<Answer> answers;

    public Question() {
    }

    public Question(int id, String questionname, List<Answer> answers) {
        super();
        this.id = id;
        this.questionname = questionname;
        this.answers = answers;
    }

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @XmlElement
    public String getQuestionname() {
        return questionname;
    }

    public void setQuestionname(String questionname) {
        this.questionname = questionname;
    }

    @XmlElement
    public List<Answer> getAnswers() {
        return answers;
    }

    public void setAnswers(List<Answer> answers) {
        this.answers = answers;
    }

}

这个xml涉及SoapBody和SoapEnvelope。任何人都可以建议在java类中进行更改,以将此xml转换为对象形式。 如何将xml解析为对象。我在这里使用JAXB。 感谢

1 个答案:

答案 0 :(得分:0)

This链接

上找到上述问题的答案

似乎问题已经回答here

谢谢