7

我正在使用 XStream,并且有一个 XML 示例:

<person>
    <firstname>Joe</firstname>
    <lastname>Walnes</lastname>
    <phone value="1234-456" />
    <fax value="9999-999" />
</person>

我想把它映射到课堂上

public class Person {

    private String firstname;
    private String lastname;
    private String phone;
    private String fax;

}

所以想法是将嵌套元素的属性映射到当前对象。我试图找到任何现成的转换器,但没有成功。我相信通过实施新的转换器可以做到这一点,但可能有人已经这样做了。或者有一个我还没有找到的解决方案。

更新:

我试图实现的想法是省略不必要的创建和映射实体。我根本不需要电话和传真实体,我只需要模型中的属性。我试图解析的 XML 模式对我来说是第三方的,我无法更改它。

4

3 回答 3

5

I don't know of a ready-to-use converter that will do it, but it's pretty trivial to write one

import com.thoughtworks.xstream.converters.*;
import com.thoughtworks.xstream.io.*;

public class ValueAttributeConverter implements Converter {
  public boolean canConvert(Class cls) {
    return (cls == String.class);
  }

  public void marshal(Object source, HierarchicalStreamWriter w, MarshallingContext ctx) {
    w.addAttribute("value", (String)source);
  }

  public Object unmarshal(HierarchicalStreamReader r, UnmarshallingContext ctx) {
    return r.getAttribute("value");
  }
}

You can attach the converter to the relevant fields using annotations

import com.thoughtworks.xstream.annotations.*;

@XStreamAlias("person")
public class Person {

    private String firstname;
    private String lastname;

    @XStreamConverter(ValueAttributeConverter.class)
    private String phone;

    @XStreamConverter(ValueAttributeConverter.class)
    private String fax;

    // add appropriate constructor(s)

    /** For testing purposes - not required by XStream itself */
    public String toString() {
      return "fn: " + firstname + ", ln: " + lastname +
             ", p: " + phone + ", f: " + fax;
    }
}

To make this work, all you need to do is instruct XStream to read the annotations:

XStream xs = new XStream();
xs.processAnnotations(Person.class);
Person p = (Person)xs.fromXML(
  "<person>\n" +
  "  <firstname>Joe</firstname>\n" +
  "  <lastname>Walnes</lastname>\n" +
  "  <phone value='1234-456' />\n" +
  "  <fax value='9999-999' />\n" +
  "</person>");
System.out.println(p);
// prints fn: Joe, ln: Walnes, p: 1234-456, f: 9999-999
于 2012-09-19T10:10:17.890 回答
4

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

如果您愿意使用 XStream 以外的库,以下是如何利用EclipseLink JAXB (MOXy)@XmlPath中的扩展。

注释允许您通过 XPath将@XmlPath您的字段/属性映射到 XML 文档中的某个位置(请参阅:http ://blog.bdoughan.com/2010/07/xpath-based-mapping.html )。

package forum12425401;

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

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

    private String firstname;
    private String lastname;

    @XmlPath("phone/@value")
    private String phone;

    @XmlPath("fax/@value")
    private String fax;

}

jaxb.properties

要将 MOXy 指定为您的 JAXB 提供程序,您需要包含一个jaxb.properties在与域模型相同的包中调用的文件,其中包含以下条目(请参阅:http ://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as -你的.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示

下面的代码会将 XML 转换为您的域模型,然后将域模型写回 XML。

package forum12425401;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Person.class);

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

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

}

输入.xml/输出

<?xml version="1.0" encoding="UTF-8"?>
<person>
   <firstname>Joe</firstname>
   <lastname>Walnes</lastname>
   <phone value="1234-456"/>
   <fax value="9999-999"/>
</person>
于 2012-09-20T20:56:29.463 回答
0

请参阅 XStream Alias 教程的“属性别名”部分:http: //x-stream.github.io/alias-tutorial.html

于 2012-09-18T19:35:47.557 回答