【问题标题】:Reading the Custom XML Processing Instruction through JAXB Unmarshelling通过 JAXB Unmarshelling 读取自定义 XML 处理指令
【发布时间】:2014-02-26 23:48:35
【问题描述】:

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

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

【问题讨论】:

  • This question 有一个答案,它显示了如何编写一个适配器来处理带有处理指令的编组和解组 JAXB。
  • 说到了使用Adapter和Writer从Java对象转换为XML时添加处理指令,我想知道在将它转换为Java对象时是否可以从XML读取处理指令。

标签: jaxb2


【解决方案1】:

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

演示

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"

【讨论】:

    【解决方案2】:

    您可以使用 JAXB 的 UnmarshallerHandler,并更新 UnmarshallerHandlerprocessingInstruction mothed。

    演示:

    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);
    }
    

    MyUnmarshallerHandlerWrapper.java 中,更新 processingInstruction 的 UnmarshallerHandler :

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

    以及所有 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();
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 2023-03-03
      • 2011-10-19
      • 2020-01-22
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多