Reputation: 19
I have a large XML file containing many key value pairs. The file contains both multi-line comments and actual data. Within the comments section, there are examples of how the data/key-value pairs should be arranged. The SAX parser that I have made successfully retrieves the keys and values from the file, but it also reads the example keys/values contained within the comments, which I do not want to happen. How can I make it so my SAX parser ignores everything within the comments section? I am not allowed to edit the file and I must use java.
Below is an example of the file that I am working with. Notice how there are data tags within the comment section. I do not want to read the sample data within these tags, but my parser records them anyways.
<?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>
Here is the code that I am using:
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;
}
}
}
Upvotes: 1
Views: 366
Reputation: 19
Well it looks like the JAXP SAX parser does in fact ignore data contained within comments. I was just interpreting my tests wrong. In my example XML file, I did not include some tags, one of which is called <reshader>
. These reshader tags also contain a <value>
tag, which my parser was picking up (which I assumed was from the comments, but it turned out to be from the reshaders).
I was able to fix my problem by adding a boolean variable called 'dataFound' that would only be set to true when a tag was found. Then within my characters method, I simply changed the if condition from if(valueFound){...}
to if(dataFound && valueFound){...}
. Finally, within the endElement()
method, I set the 'dataFound' variable to false whenever a </data>
tag was found.
Upvotes: 0