3

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?

  • [This question](http://stackoverflow.com/questions/6931520/how-to-add-xml-processing-instructions-during-jaxb-marshal) has an answer which shows how to write an Adapter to deal with marshalling and unmarshalling JAXB with processing instructions. – helderdarocha Feb 26 '14 at 23:56
  • That talks about adding the Processing Instruction when converting from Java Object to XML using Adapter and Writer, I wanted to know if i can read the processing Instruction from XML when converting it to Java Object. – user3358226 Feb 27 '14 at 00:06

2 Answers2

0

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"
bdoughan
  • 147,609
  • 23
  • 300
  • 400
0

You can use UnmarshallerHandler of JAXB, and update processingInstruction mothed of UnmarshallerHandler.

DEMO:

private static void test( String fileName ) throws Exception {
    JAXBContext context = JAXBContext.newInstance( Customer.class );

    Unmarshaller unmarshaller = context.createUnmarshaller();

    UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();

    unmarshallerHandler = new MyUnmarshallerHandlerWrapper(unmarshallerHandler);

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware( true );

    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    xmlReader.setContentHandler( unmarshallerHandler );
    xmlReader.parse(new InputSource( new FileInputStream( fileName ) ) );

    Customer myObject= (Customer)unmarshallerHandler.getResult();
    System.out.println(myObject);
}

In MyUnmarshallerHandlerWrapper.java ,update processingInstruction mothed of UnmarshallerHandler :

  @Override
  public void processingInstruction(String target, String data)
      throws SAXException {
    System.out.println("<?" + target + " " + data + "?>");
  }

and all of MyUnmarshallerHandlerWrapper.java :

import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshallerHandler;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;


public class MyUnmarshallerHandlerWrapper implements UnmarshallerHandler {

  private UnmarshallerHandler handle;

  public MyUnmarshallerHandlerWrapper(UnmarshallerHandler handle) {
    this.handle = handle;
  }

  @Override
  public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
    handle.characters(arg0, arg1, arg2);
  }

  @Override
  public void endDocument() throws SAXException {
    handle.endDocument();
  }

  @Override
  public void endElement(String arg0, String arg1, String arg2)
      throws SAXException {
    handle.endElement(arg0, arg1, arg2);
  }

  @Override
  public void endPrefixMapping(String arg0) throws SAXException {
    handle.endPrefixMapping(arg0);
  }

  @Override
  public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
      throws SAXException {
    handle.ignorableWhitespace(arg0, arg1, arg2);
  }

  @Override
  public void processingInstruction(String target, String data)
      throws SAXException {
    System.out.println("<?" + target + " " + data + "?>");
  }

  @Override
  public void setDocumentLocator(Locator arg0) {
    handle.setDocumentLocator(arg0);
  }

  @Override
  public void skippedEntity(String arg0) throws SAXException {
    handle.skippedEntity(arg0);
  }

  @Override
  public void startDocument() throws SAXException {
    handle.startDocument();
  }

  @Override
  public void startElement(String arg0, String arg1, String arg2,
      Attributes arg3) throws SAXException {
    handle.startElement(arg0, arg1, arg2, arg3);
  }

  @Override
  public void startPrefixMapping(String arg0, String arg1)
      throws SAXException {
    handle.startPrefixMapping(arg0, arg1);
  }

  @Override
  public Object getResult() throws JAXBException, IllegalStateException {
    return handle.getResult();
  }

}
tangzhi
  • 1
  • 1