通过JAXB Unmarshelling读取自定义XML处理指令 [英] Reading the Custom XML Processing Instruction through JAXB Unmarshelling
本文介绍了通过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"?>
在unmarshelling中的上述xml中,unmarshelling后不存在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屋!
查看全文