在使用SAX解析XML文件时如何保留注释和XML声明?

时间:2015-11-18 10:42:31

标签: java xml sax saxparser

我有一个简单的任务:

我想阅读XML文件并尽可能完整地返回它。使用以下代码时,还有两个问题:

  1. 评论已删除
  2. 我无法访问XML-Declaration
  3. Java代码:

    package com.stackoverflow.tests;
    
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class XmlParsing {
    
      public static void main(String[] args) {
    
        StringBuffer b = new StringBuffer();
    
        try {
    
          SAXParserFactory factory = SAXParserFactory.newInstance();
          SAXParser saxParser = factory.newSAXParser();
    
          DefaultHandler handler = new DefaultHandler() {
    
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
    
              b.append("<" + qName + attributesToString(attributes) + ">");
            } // END: startElement()
    
    
    
            @Override
            public void endElement(String uri, String localName, String qName)
                throws SAXException {
    
              b.append("</" + qName + ">");
            } // END: endElement
    
    
    
            @Override
            public void characters(char ch[], int start, int length)
                throws SAXException {
    
              b.append(new String(ch, start, length));
    
            } // END: characters()
    
    
    
          }; // END: DefaultHandler
    
          saxParser.parse("./src/main/ressources/XmlTest/validWithAttributesCommentsInlineElements.xml", handler);
    
          System.out.println(b.toString());
    
        } catch (Exception e) {
          e.printStackTrace();
    
        } // END: try
    
      } // END: main
    
    
    
      public static String attributesToString(Attributes a) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < a.getLength(); i++) {
          sb
            .append(" ")
            .append(a.getQName(i))
            .append("=\"")
            .append(a.getValue(i))
            .append("\"");
        }
        return sb.toString();
      }
    
    
    
    } // END: Class XmlParsing
    

    我解析了下面的XML文件......:

    <?xml version="1.0" encoding="UTF-8"?>
    <A attr="1" aaa="2">
        <F>general</F>
        <B test="3">
            <C>element 1</C>
            <C>element 2</C>
            <C>element 3</C>
        </B>
        <D>general</D>
        <E>general</E>
    
        <inline-element/>
        <inline-element with="attributes"/>
    
        <!-- Comment -->
    
        <inline-element />
        <inline-element with="attributes" />
    
    </A>
    

    得到:

    <A attr="1" aaa="2">
        <F>general</F>
        <B test="3">
            <C>element 1</C>
            <C>element 2</C>
            <C>element 3</C>
        </B>
        <D>general</D>
        <E>general</E>
    
        <inline-element></inline-element>
        <inline-element with="attributes"></inline-element>
    
    
    
        <inline-element></inline-element>
        <inline-element with="attributes"></inline-element>
    
    </A>
    

    <elem />成为<elem></elem>对我来说没问题,但我真的想要访问XML声明和评论。

1 个答案:

答案 0 :(得分:1)

要在看到评论时访问事件,您需要使用Lexcial Handler。见https://docs.oracle.com/javase/tutorial/jaxp/sax/events.html

.skip