java如何使用JAXP SAX解析器忽略XML注释中包含的数据?
我有一个包含许多键值对的大型XML文件。该文件包含多行注释和实际数据。在评论部分,有一些示例说明了应该如何安排数据/键值对。我制作的SAX解析器成功地从文件中检索键和值,但它也读取注释中包含的示例键/值,这是我不希望发生的。如何使我的SAX解析器忽略注释部分中的所有内容?我不允许编辑文件,我必须使用java
下面是我正在使用的文件的一个示例。注意注释部分中的数据标记。我不想读取这些标记中的样本数据,但我的解析器无论如何都会记录它们
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data> **I DO NOT WANT TO READ THIS**
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
-->
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AmountUnits" xml:space="preserve">
<value>Amount/Units</value>
</data>
</root>
以下是我正在使用的代码:
public class xmlPropertiesBuilder extends DefaultHandler {
private boolean valueFound;
public void readXMLFile(File xmlFile) throws SAXException, IOException, ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(xmlFile, this);
valueFound = false;
}
@Override
public void startDocument() throws SAXException {
System.out.println("Start Document");
}
@Override
public void endDocument() throws SAXException {
System.out.println("End Document");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equals("data")){
System.out.println("Start Element: " + qName);
System.out.println("Key: " + attributes.getValue("name"));
} else if(qName.equals("value")){
valueFound = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equals("data")){
System.out.println("End Element: " + qName + "\n");
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if(valueFound){
System.out.println("Value: " + new String(ch, start, length));
valueFound = false;
}
}
}
# 1 楼答案
看起来JAXP SAX解析器实际上忽略了注释中包含的数据。我只是误解了我的测试。在我的示例XML文件中,我没有包含一些标记,其中一个名为
<reshader>
。这些reshader标记还包含一个<value>
标记,我的解析器正在提取这个标记(我假设是从注释中提取的,但事实证明是从reshader中提取的)我可以通过添加一个名为“dataFound”的布尔变量来解决问题,该变量只有在找到标记时才会设置为true。然后在characters方法中,我简单地将if条件从
if(valueFound){...}
更改为if(dataFound && valueFound){...}
。最后,在endElement()
方法中,每当找到</data>
标记时,我就将'dataFound'变量设置为false