Reading the Custom XML Processing Instruction through JAXB Unmarshelling

前端 未结 2 1977
自闭症患者
自闭症患者 2021-01-06 15:53

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



        
相关标签:
2条回答
  • 2021-01-06 16:34

    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"
    
    0 讨论(0)
  • 2021-01-06 16:39

    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();
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题