gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:47:04 25 4
gpt4 key购买 nike

我有一个简单的任务:

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

  1. 评论已删除
  2. 我无权访问 XML 声明

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 声明和注释。

最佳答案

为了在看到评论时访问事件,您需要使用词法处理程序。请参阅https://docs.oracle.com/javase/tutorial/jaxp/sax/events.html

// Implement a handler
LexialHandler handler = new LexicalHandler() {
@Override
public void comment(char[] ch, int start, int length) throws SAXException {
// ...
}
}

// Use the handler

SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler",
handler);

关于java - 使用 SAX 解析 XML 文件时如何保留注释和 XML 声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33777878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com