java - 如何使用 JAXB 注释将此 XML 转换为 Java 对象?

标签 java xml jaxb

这个 XML 设计的很痛苦,但无论如何都需要处理。 我无法将其设为 <ack> 的列表对象,因为每个对象都有一组不同的标签,对吗? 请帮忙!

<frm>
   <version>1.1.0</version>
   <ack>
      <id_1>--</id_1>
      <pow_1 />
      <temp_1 />
      <state_1>fixed</state_1>
   </ack>
   <ack>
      <id_2>0000C6F5</id_2>
      <pow_2>0w</pow_2>
      <temp_2>---</temp_2>
      <state_2>fixed</state_2>
   </ack>
   <ack>
      <id_3>--</id_3>
      <pow_3 />
      <temp_3 />
      <state_3>fixed</state_3>
   </ack>
   <ack>
      <id_4>--</id_4>
      <pow_4 />
      <temp_4 />
      <state_4>fixed</state_4>
   </ack>
   <ack>
      <id_5>--</id_5>
      <pow_5 />
      <temp_5 />
      <state_5>fixed</state_5>
   </ack>
   <ack>
      <id_6>--</id_6>
      <pow_6 />
      <temp_6 />
      <state_6>fixed</state_6>
   </ack>
   <ack>
      <id_7>--</id_7>
      <pow_7 />
      <temp_7 />
      <state_7>fixed</state_7>
   </ack>
   <ack>
      <id_8>--</id_8>
      <pow_8 />
      <temp_8 />
      <state_8>fixed</state_8>
   </ack>
   <ack>
      <id_9>--</id_9>
      <pow_9 />
      <temp_9 />
      <state_9>fixed</state_9>
   </ack>
   <ack>
      <id_10>--</id_10>
      <pow_10 />
      <temp_10 />
      <state_10>fixed</state_10>
   </ack>
</frm>

最佳答案

下面是如何结合使用 JAXB 和 StAX 来支持此用例。

演示代码

演示

我们可以使用 StreamReaderDelegate 来更改元素名称的报告方式。我们将使用它来删除 _ 及其后的所有内容。

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.transform.stream.StreamSource;

public class Demo {

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

        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource source = new StreamSource("src/forum18096385/input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(source);
        xsr = new StreamReaderDelegate(xsr) {

            @Override
            public String getLocalName() {
                String localName = super.getLocalName();
                int underScoreIndex = localName.indexOf('_');
                if(underScoreIndex >= 0) {
                    return localName.substring(0, underScoreIndex);
                }
                return localName;
            }

        };

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Frm frm = (Frm) unmarshaller.unmarshal(xsr);

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

}

输入.xml

您问题中的 XML。

输出

由于我们将映射到修改后的 XML,如果我们要编码结果,它看起来会与输入不同。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<frm>
    <version>1.1.0</version>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>0000C6F5</id>
        <pow>0w</pow>
        <temp>---</temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
    <ack>
        <id>--</id>
        <pow></pow>
        <temp></temp>
        <state>fixed</state>
    </ack>
</frm>

Java 模型

下面是我们将映射到修改后的 XML 的 Java 模型。

框架

import java.util.List;
import javax.xml.bind.annotation.*;

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

    private String version;
    private List<Ack> ack;

}

确认

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Ack {

    private String id;
    private String pow;
    private String temp;
    private String state;

}

关于java - 如何使用 JAXB 注释将此 XML 转换为 Java 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18096385/

相关文章:

java - 获取 javax.servlet.ServletException : when running my eclipse on jboss?

java - 如果我在 onCreate 方法中添加 GraphViewer 代码,应用程序会崩溃

android - 选择器与 android :drawable ="@color/transparent" attribute

java - 为同一 xml 命名空间中的多个模式设置 xjc 的 maven 构建?

java - Weak/Soft/Phantom Reference清算的并发保证

java - 派生方法在应该返回 newList 时正在更改原始列表

java - 如何更改 XML 模式中类的访问修饰符

java - JAXB 注释不适用于 Jackson 消息转换器

java - 是否可以使用 Hibernate 将一个 Java 对象映射到具有 FK 关系的多个表?

android - 如何在横向模式下制作 UI