[英]How to map xml attribute values to java class attributes using jaxB or any other better method?
<?xml version="1.0" encoding="UTF-8"?>
<tns:class xmlns:tns="http://www.example.org/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/test test.xsd ">
<attr name="bookid" primary="true"/>
<attr name="bookname" primary="false"/>
<attr name="noOfPages" primary="false"/>
<attr name="auth_name" primary="false"/>
</tns:class>
package com.srl.rotelearning.test;
public class Book {
private int bookId;
private String bookname;
private int noOfPages;
public int getBookId() {
return bookId;
}
public int getNoOfPages() {
return noOfPages;
}
public void setNoOfPages(int noOfPages) {
this.noOfPages = noOfPages;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
}
我嘗試使用JAXB,但我認為我們可以將屬性名稱映射到類屬性。 如何將xml屬性值映射到我的類屬性? plz給出詳細的ans作為使用xml的新手謝謝:)
在EclipseLink JAXB(MOXy)中,我們通過@XmlPath
擴展提供此行為
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
@XmlPath("attr[@name='bookid']/text()")
private int bookId;
@XmlPath("attr[@name='bookname']/text()")
private String bookname;
@XmlPath("attr[@name='noOfPages']/text()")
private int noOfPages;
}
欲獲得更多信息
我在博客上寫了更多關於這個用例的文章:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.