如何使用xstream读取xml中的注释 [英] how to read comments in xml using xstream

查看:299
本文介绍了如何使用xstream读取xml中的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用XStream和Java在解析XML注释时读取XML注释。

Is there a way to read xml comments while parsing it using XStream with Java.

<!--
 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进行序列化/反序列化。我需要将注释作为注释保存在POJO上,以便可以在UI中显示。

I currently use XStream to serialize/ de-serialize kind of xml above. I need to save comments as annotations on my POJO so that i can show it in UI.

在XStream中我找不到任何内容。

I could not find anything in XStream for that.

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

DOM has DocumentBuilderFactory.setIgnoringComments(boolean) that let you include comments in the DOM tree and you can distinguish between type of Nodes.

类似地,C#具有 XmlReaderSettings.IgnoreComments

推荐答案

XStream无法处理XML注释。

XStream can not process XML comments to my knowledge.

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

Here is another approach, which uses the 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) {
    }
  }
}

这篇关于如何使用xstream读取xml中的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆