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"?>
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?
You can use JAXB with StAX to get the Processing Instruction data:
Demo
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());
}
}
Output
CustomExtn
Number="AC7654321" LastName="Szychlinski"