通过JAXB解组阅读定制XML处理指令

用户名

通过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"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章