【问题标题】:JAXB XML; How to fetch an attribute of an element without needing to create a pojo of the element?JAXB XML;如何在不需要创建元素的 pojo 的情况下获取元素的属性?
【发布时间】:2021-10-29 08:04:10
【问题描述】:

假设我只需要 'child' 的几个属性

<parent>
   <child name="Child" age="1" />
</parent>

我不想创建一个“子”类。我只想包装它并获取属性。这可能吗?我可以使用任何包装器注释吗?

【问题讨论】:

  • 到目前为止的java代码是什么?你试过什么?
  • @Presto 以下提供的代码对您有用吗?
  • @BATMAN_2008 这当然有效,但我希望有更多类似于存储所有内容的 JAXB 对象。而不必为子元素创建一个类。
  • 如果是这种情况,那么您可以使用 @XmlAnyElementsList&lt;Object&gt; 这将存储所有内容。
  • @Presto 我已经更新了我的代码,并为您提供了另一种方法。

标签: java xml parsing jaxb


【解决方案1】:

嗯,你可以做到,但如果你不采用传统的方法,那么它会变得非常棘手。这是您可以尝试的代码:

XML:

<parent>
    <child name="Child" age="1" />
</parent>

根:

@XmlRootElement(name = "parent")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Root {
    @XmlJavaTypeAdapter(Adapter.class)
    private String child;
}

孩子:

@Data
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Child {
    @XmlAttribute
    private String name;

    @XmlAttribute
    private String age;
}

适配器:

public class Adapter extends XmlAdapter<Child, String> {

    public String unmarshal(Child pC) throws Exception {
        System.out.println(pC.toString());
        return null;
    }

    public Child marshal(String pC) throws Exception {
        return null;
    }
}

主要:

public class Main {
    public static void main(String[] args) throws JAXBException, XMLStreamException {
        final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("sample.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
        final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
        System.out.println(root.toString());
    }
}

输出:

Child(name=Child, age=1)

更新为存储没有子类的所有内容

XML:

<parent>
    <child name="Child" age="1" />
</parent>

根:

@Data
@XmlRootElement(name = "parent")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
    @XmlAnyElement
    private List<Object> any;
}

主要:

public class Main {
    public static void main(String[] args) throws JAXBException, XMLStreamException {
        final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("sample.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
        final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
        System.out.println(root.toString());

        Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "US-ASCII");
        //marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", new XmlCharacterHandler());
        marshaller.marshal(root, System.out);
    }
}

XML:

Root(any=[[child: null]])
<?xml version="1.0" encoding="US-ASCII"?>
<parent>
   <child age="1" name="Child"/>
</parent>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多