使用 XStream 读取元素属性值 [英] Reading Element attribute value using XStream

查看:116
本文介绍了使用 XStream 读取元素属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法查找元素的属性值.我的 XML 是

I am not able lookup attribute value of an element. My XML is

<Person>
          <BirthDate>2008-01-04</BirthDate>
          <FirstName>Affo</FirstName>
          <Gender tc="200">Male</Gender>
          <LastName></LastName>
          <Occupation>false</Occupation>
          <Age>4</Age>
</Person>

我对 Male</Gender> 感兴趣.我的 POJO 看起来像这样:

I am interested in <Gender tc="200">Male</Gender>. My POJO looks like this:

    private String FirstName;
    private String LastName;
    private String Occupation;
    @XStreamAsAttribute
    @XStreamAlias("tc")
    private String genderTC;
    private String Gender;
    private String birthDate;
    private int age;

来自 XML 是

            XStream stream = new XStream(new DomDriver());
        stream.processAnnotations(PersonType.class);
        PersonType person = (PersonType) stream.fromXML(file);

        System.out.println(person.getFirstName());
        System.out.println(person.getGenderTC());
        System.out.println(person.getGender());

这里对于 person.getGenderTC() 我得到空值.有趣的部分是当我颠倒过程并使用相同的 PersonType pojo 生成 xml 时,我得到了以下 XML:

Here for person.getGenderTC() I am getting null. Interesting part is when I reversed the process and generated the xml using same PersonType pojo, I got following XML:

<Person tc="111">
  <FirstName>Himanshu</FirstName>
  <Gender>M</Gender>
  <Age>28</Age>
</Person>

推荐答案

@BlaiseDoughan 太好了,感谢您的支持.你是否可以请告诉我如何将 EclipseLink MOXy 与我的项目集成不使用 jaxb.properties?要包含哪些库/JAR?在两者之间,我知道 EclipseLink JAXB (MOXy) 实现来获得属性.我唯一的预感是 jaxb.properties 文件.

@BlaiseDoughan That's great and I appreciate your support. Can you please tell me how can I integrate EclipseLink MOXy with my project without using jaxb.properties? What are the libraries/JARs to include? In between I am aware of EclipseLink JAXB (MOXy) implementation to get attribute. My only hunch is jaxb.properties file.

以下是您如何使用 MOXy 的 @XmlPath 注释来完成您正在寻找的映射:

Below is how you could use MOXy's @XmlPath annotation to do the mapping you are looking for:

package forum11417620;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

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

    @XmlElement(name="FirstName")
    private String firstName;

    @XmlElement(name="LastName")
    private String lastName;

    @XmlElement(name="Occupation")
    private String occupation;

    @XmlPath("Gender/@tc")
    private String genderTC;

    @XmlPath("Gender/text()")
    private String gender;

    @XmlElement(name="BirthDate")
    private String birthDate;

    @XmlElement(name="Age")
    private int age;

}

演示

下面是一个示例,说明如何在没有 jaxb.properties 文件的情况下引导 MOXy JAXBContext.

Below is an example of how you can bootstrap a MOXy JAXBContext without a jaxb.properties file.

package forum11417620;

import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {Person.class}, null);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11417620/input.xml");
        Person person = (Person) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(person, System.out);
    }

}

input.xml/输出

<?xml version="1.0" encoding="UTF-8"?>
<Person>
   <FirstName>Affo</FirstName>
   <LastName></LastName>
   <Occupation>false</Occupation>
   <Gender tc="200">Male</Gender>
   <BirthDate>2008-01-04</BirthDate>
   <Age>4</Age>
</Person>

必需的二进制文件(来自 http://www.eclipse.org/eclipselink/下载/)

选项 #1 - EclipseLink JAR(来自安装程序 Zip)

  • eclipselink.jar

选项 #2 - MOXy 捆绑包(来自 OSGi Bundles Zip)

  • org.eclipse.persistence.moxy.jar
  • org.eclipse.persistence.core.jar
  • org.eclipse.persistence.asm.jar

Maven

我在 Git Hub 上有示例 pom.xml 文件作为我博客中一些示例的一部分:

I have example pom.xml files on Git Hub as part of some examples from my blog:

这篇关于使用 XStream 读取元素属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆