【问题标题】:how to read comments in xml using xstream如何使用 xstream 读取 xml 中的注释
【发布时间】:2012-10-31 06:12:22
【问题描述】:

有没有办法在使用 XStream 和 Java 进行解析时读取 xml cmets。

<!--
 Mesh:  three-dimensional box 100m x 50m x 50m Created By Sumit Purohit on 
 for a stackoverflow query.
-->  
<ParameterList name="Mesh">  
 <Parameter name="Domain Low Corner" type="Array double" value="{0.0, 0.0,  0.0}" /> 
 <Parameter name="Domain High Corner" type="Array double" value="{100.0, 50.0,50.0}" /> 
</ParameterList>

我目前使用 XStream 来序列化/反序列化上述类型的 xml。我需要将 cmets 作为注释保存在我的 POJO 上,以便我可以在 UI 中显示它。

我在 XStream 中找不到任何东西。

DOM 有DocumentBuilderFactory.setIgnoringComments(boolean),可以让您在 DOM 树中包含 cmets,并且可以区分节点类型。

C# 也有XmlReaderSettings.IgnoreComments

【问题讨论】:

    标签: java comments xstream


    【解决方案1】:

    尝试使用LexicalHandler API 来解析 XML 中的 CData 和 Comments。

    【讨论】:

      【解决方案2】:

      据我所知,XStream 无法处理 XML cmets。

      这是另一种方法,它使用 LexicalHandler API:

      import org.xml.sax.*;
      import org.xml.sax.ext.*;
      import org.xml.sax.helpers.*;
      
      import java.io.IOException;
      
      public class ReadXMLFile implements LexicalHandler {
      
        public void startDTD(String name, String publicId, String systemId)
            throws SAXException {
        }
      
        public void endDTD() throws SAXException {
        }
      
        public void startEntity(String name) throws SAXException {
        }
      
        public void endEntity(String name) throws SAXException {
        }
      
        public void startCDATA() throws SAXException {
        }
      
        public void endCDATA() throws SAXException {
        }
      
        public void comment(char[] text, int start, int length)
            throws SAXException {
      
          System.out.println("Comment: " + new String(text, start, length));
        }
      
        public static void main(String[] args) {
          // set up the parser
          XMLReader parser;
          try {
            parser = XMLReaderFactory.createXMLReader();
          } catch (SAXException ex1) {
            try {
              parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
            } catch (SAXException ex2) {
              return;
            }
          }
      
          try {
            parser.setProperty("http://xml.org/sax/properties/lexical-handler",new ReadXMLFile()
            );
          } catch (SAXNotRecognizedException e) {
            System.out.println(e.getMessage());
            return;
          } catch (SAXNotSupportedException e) {
            System.out.println(e.getMessage());
            return;
          }
      
          try {
            parser.parse("xmlfile.xml"); // <----  Path to XML file
          } catch (SAXParseException e) { // well-formedness error
            System.out.println(e.getMessage());
          } catch (SAXException e) { 
            System.out.println(e.getMessage());
          } catch (IOException e) {
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-18
        • 1970-01-01
        • 2012-08-17
        • 2016-12-10
        相关资源
        最近更新 更多