ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何使用SAX Java解析器读取注释文本

2019-11-11 23:00:57  阅读:263  来源: 互联网

标签:xml sax java parsing


我只想使用Java中的SAX解析器读取XML文件中对象标签的注释.

这是我文件的摘要:

<!-- Object Seed term: day, WikiTitle: day-->
<object id="15155220" name="solar day, twenty-four hour period, 24-hour interval, mean solar day, twenty-four hours, si day, día, days, si days, day duration, day, civil day">
    <!-- class: "calendar day" -->
    <class id="15157041" name="calendar day, civil day"></class>
    <!-- class: "unit of time" -->
    <class id="15154774" name="time units, unit of time, time unit, units of time"></class>
    <!-- class: "" -->
    <class id="15113229" name="period of time, time period, period"></class>
    <!-- class: "" -->
    <class id="00000000" name="time"></class>
    <genericPhysicalDescription>
        <!-- hasPart: "" -->
        <hasPart id="15228378" name="hour, time of day"></hasPart>
        <!-- hasPart: "" -->
        <hasPart id="15157225" name="day"></hasPart>
        <!-- partOf: "calendar" -->
        <partOf id="15173479" name="calendrics, calendar, dating style, calendarist, calendars, birthday calendar, calendar strip, secular calendar, calandar, agriculture calendar, calendar system, criminal calendar"></partOf>
        <!-- partOf: "" -->
        <partOf id="15206296" name="month"></partOf>
        <!-- partOf: "" -->
        <partOf id="15157225" name="day"></partOf>
    </genericPhysicalDescription>
</object>

解决方法:

javax.xml.parsers.SAXParser不支持阅读注释.它忽略了它们.

使用org.xml.sax.XMLReader进行解析时,可以使用org.xml.sax.ext.LexicalHandler捕获注释.请参见示例another stackoverflow posttutorial at Oracle.

如果要将注释连接到紧随其后的元素,则可以另外将org.xml.sax.ContentHandler传递到解析器并由此跟踪其他XML内容.我修改了上面提到的代码以仅打印该对象元素,并在其后立即添加了注释:

import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;

import java.io.IOException;

public class Test implements LexicalHandler, ContentHandler {

  private String  lastComment;

  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 {
    this.lastComment = new String(text, start, length).trim();
  }

  public void characters(char[] ch, int start, int length) {
  }
  public void endDocument() {
  }
  public void endElement(String uri, String localName, String qName) {
  }
  public void endPrefixMapping(String prefix) {
  }
  public void ignorableWhitespace(char[] ch, int start, int length) {
  }
  public void processingInstruction(String target, String data) {
  }
  public void setDocumentLocator(Locator locator) {
  }
  public void skippedEntity(String name) {
  }
  public void startDocument() {
  }
  public void startElement(String uri, String localName, String qName, Attributes atts) {
    if (localName == "object") {
      if (this.lastComment != null) {
        System.out.println("Element object with comment found: \"" + this.lastComment + "\"");
        this.lastComment = null;
      }
    } else {
      this.lastComment = null;
    }
  }
  public void startPrefixMapping(String prefix, String uri) {
  }

  public static void main(String[] args) {
    Test test = new Test();
    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", test);
    } catch (SAXNotRecognizedException e) {
      System.out.println(e.getMessage());
      return;
    } catch (SAXNotSupportedException e) {
      System.out.println(e.getMessage());
      return;
    }

    parser.setContentHandler(test);

    try {
      parser.parse("test.xml");
    } catch (SAXParseException e) {
      System.out.println(e.getMessage());
    } catch (SAXException e) { 
      System.out.println(e.getMessage());
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
  }
}

将此代码保存到“ Test.java”,并将您的XML内容保存到“ test.xml”.编译并执行后,它应该为您提供以下输出:

$javac Test.java 
$java Test 
Element object with comment found: "Object Seed term: day, WikiTitle: day"

标签:xml,sax,java,parsing
来源: https://codeday.me/bug/20191111/2022870.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有