I didn’t work very much with XML, so perhaps my ignorance of the correct terminology hurts me in my search for how to do this. I have a piece of code below which I use to parse an XML file similar to the one below. The problem is that it only accepts XML values in <Tag>Value</Tag>, but not for below, where I need to get the value TagValue, which in this case will be "Russell Diamond".
I would appreciate if anyone could offer help on how to get custom values like this. Thank.
<Tag TagName="#Subject" TagDataType="Text" TagValue="Russell Diamond"/>
Used fragment:
public void printElementNames(String fileName) throws IOException {
FileWriter fstream = new FileWriter("/home/user/Desktop/readEDRMtest.txt");
final BufferedWriter out = new BufferedWriter(fstream);
try {
SAXParserFactory parserFact = SAXParserFactory.newInstance();
SAXParser parser = parserFact.newSAXParser();
System.out.println("XML Elements: ");
DefaultHandler handler = new DefaultHandler() {
public void startElement(String uri, String lName, String ele,
Attributes attributes) throws SAXException {
System.out.println(ele);
try {
out.write(ele);
} catch (IOException e) {
e.printStackTrace();
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println("Value : "
+ new String(ch, start, length));
try {
out.write("Value : "
+ new String(ch, start, length));
} catch (IOException e) {
e.printStackTrace();
}
}
};
source
share