通过 JAXB Unmarshelling 读取自定义 XML 处理指令 [英] Reading the Custom XML Processing Instruction through JAXB Unmarshelling

查看:26
本文介绍了通过 JAXB Unmarshelling 读取自定义 XML 处理指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to read the custom xml processing instruction when unmarshelling through JAXB. Example,

<?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 类中阅读此内容?

In the above xml when unmarshelling, the CustomExtn is not present after unmarshelling. Is there a way i can read this in the Java Class?

推荐答案

您可以使用 JAXB 和 StAX 来获取处理指令数据:

You can use JAXB with StAX to get the Processing Instruction data:

演示

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"

这篇关于通过 JAXB Unmarshelling 读取自定义 XML 处理指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆