gpt4 book ai didi

jaxb2 - 通过 JAXB 解码读取自定义 XML 处理指令

转载 作者:行者123 更新时间:2023-12-05 07:59:22 25 4
gpt4 key购买 nike

有没有办法在通过 JAXB 解码时读取自定义 xml 处理指令。例如,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer >
<id>100</id>
<age>40</age>
<name>Sachin</name>
</customer>
<?CustomExtn Number="AC7654321" LastName="Szychlinski"?>

在上面的 xml 中,当解码时,CustomExtn 在解码后不存在。有什么方法可以在 Java 类中读取它吗?

最佳答案

您可以将 JAXB 与 StAX 结合使用来获取处理指令数据:

演示

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

public class Demo {

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

XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("src/forum22056085/input.xml"));
xsr.next(); // Advance to root element

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

System.out.println(xsr.getPITarget());
System.out.println(xsr.getPIData());
}

}

输出

CustomExtn
Number="AC7654321" LastName="Szychlinski"

关于jaxb2 - 通过 JAXB 解码读取自定义 XML 处理指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22056085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com