首页
\
问答
\
是否可以使用JAXB基于某些属性值获取XML元素?(Is it possible to get XML elements based on some attribute value/s using JAXB?)
是否可以使用JAXB基于某些属性值获取XML元素?(Is it possible to get XML elements based on some attribute value/s using JAXB?)
我使用名为
Height.xml
JAXB创建了一个XML文件。 结构如下所示。<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <heightList> <measurement id="5"> <height>4'4"</height> </measurement> <measurement id="4"> <height>4'3"</height> </measurement> <measurement id="3"> <height>4'2"</height> </measurement> <measurement id="2"> <height>4'1"</height> </measurement> <measurement id="1"> <height>4'0"</height> </measurement> </heightList>
可以将此XML文件解组为
List<Height>
,如下所示。JAXBContext jaxb=JAXBContext.newInstance(HeightList.class); File file=new File("Height.xml"); List<Height> heightList = ((HeightList) jaxb.createUnmarshaller().unmarshal(file)).getList();
是否可以根据其属性
id
获取XML元素,以便取代List<Height>
,只获取一个Height
类型的对象?id
属性是唯一的。
相关课程:
高度:
public final class Height implements Serializable { private Integer id; private String height; private static final long serialVersionUID = 1L; public Height() {} @XmlAttribute(name = "id") public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } }
HeightList:
@XmlRootElement(name = "heightList") @XmlAccessorType(XmlAccessType.PROPERTY) public final class HeightList implements Serializable { @XmlElement(name="measurement") private List<Height>list; private static final long serialVersionUID = 1L; public HeightList() {} public HeightList(List<Height> list) { this.list = list; } public List<Height> getList() { return list; } }
I have created an XML file using JAXB named
Height.xml
. The structure looks like the following.<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <heightList> <measurement id="5"> <height>4'4"</height> </measurement> <measurement id="4"> <height>4'3"</height> </measurement> <measurement id="3"> <height>4'2"</height> </measurement> <measurement id="2"> <height>4'1"</height> </measurement> <measurement id="1"> <height>4'0"</height> </measurement> </heightList>
This XML file can be unmarshalled to a
List<Height>
as follows.JAXBContext jaxb=JAXBContext.newInstance(HeightList.class); File file=new File("Height.xml"); List<Height> heightList = ((HeightList) jaxb.createUnmarshaller().unmarshal(file)).getList();
Is it possible to get an XML element based on its attribute
id
so that instead ofList<Height>
, only one object of typeHeight
is fetched? Theid
attribute is unique.
The associated classes:
Height:
public final class Height implements Serializable { private Integer id; private String height; private static final long serialVersionUID = 1L; public Height() {} @XmlAttribute(name = "id") public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } }
HeightList:
@XmlRootElement(name = "heightList") @XmlAccessorType(XmlAccessType.PROPERTY) public final class HeightList implements Serializable { @XmlElement(name="measurement") private List<Height>list; private static final long serialVersionUID = 1L; public HeightList() {} public HeightList(List<Height> list) { this.list = list; } public List<Height> getList() { return list; } }
原文:
更新时间:2022-08-27 22:08