The SAXParser uses a Handler to go over each node and output the associated name and value at your discretion. You may modify the Handler to make it send you the parsed result in a different way.
import java.xml.parsers.SAXParser;
import java.xml.parsers.SAXParserFactory;
public class XMLParser {
public void saxParse(String xmlStr) {
try {
InputSource source = new InputSource(new StringReader(xmlStr));
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
MyHandler dHandler = new MyHandler();
parser.parse(source, dHandler);
} catch (ParserConfigurationException e) {
e.printStackTrace(System.out);
} catch (SAXException e) {
e.printStackTrace(System.out);
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
private class MyHandler extends DefaultHandler {
boolean isName = false;
boolean isPrice = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("store")) {
String value = attributes.getValue("name");
System.out.println("Node name : "+qName+"; value: " + value);
} else if (qName.equalsIgnoreCase("fruit")) {
System.out.println(" "+qName);
} else if (qName.equalsIgnoreCase("name")) {
isName = true;
} else if (qName.equalsIgnoreCase("price")) {
isPrice = true;
}
}
@Override
public void endElement(String uri,
String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("fruit")) {
System.out.println("End Element :" + qName);
}
}
@Override
public void characters(char[] ch,
int start, int length) throws SAXException {
if (isName) {
System.out.println("Name: "+ new String(ch, start, length));
isName = false;
} else if (isPrice) {
System.out.println("Price: " + new String(ch, start, length));
isPrice = false;
}
}
}
public static void main(String[] args) {
StringBuilder xml = new StringBuilder();
xml.append("<store name='Deliciously Healthy'>")
.append("<fruit>")
.append("<name>Apple</name><price>1.68</price>")
.append("</fruit><fruit>")
.append("<name>Orange</name><price>0.89</price>")
.append("</fruit>")
.append("</store>");
XMLParser xmlParser = new XMLParser();
xmlParser.saxParse(xml.toString());
}
}
The output:
Node name : store; value: Deliciously Healthy
fruit
Name: Apple
Price: 1.68
End Element :fruit
fruit
Name: Orange
Price: 0.89
End Element :fruit
If you get compile time warnings such as "warning: com.sun.org.apache.xerces.internal.parsers.SAXParser is Sun proprietary API and may be removed in a future release", you may consider use the SAXParser and the DocumentBuilder in the java.xml.parsers package.
import java.xml.parsers.SAXParser;
import java.xml.parsers.SAXParserFactory;
public class XMLParser {
public void saxParse(String xmlStr) {
try {
InputSource source = new InputSource(new StringReader(xmlStr));
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
MyHandler dHandler = new MyHandler();
parser.parse(source, dHandler);
} catch (ParserConfigurationException e) {
e.printStackTrace(System.out);
} catch (SAXException e) {
e.printStackTrace(System.out);
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
private class MyHandler extends DefaultHandler {
boolean isName = false;
boolean isPrice = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("store")) {
String value = attributes.getValue("name");
System.out.println("Node name : "+qName+"; value: " + value);
} else if (qName.equalsIgnoreCase("fruit")) {
System.out.println(" "+qName);
} else if (qName.equalsIgnoreCase("name")) {
isName = true;
} else if (qName.equalsIgnoreCase("price")) {
isPrice = true;
}
}
@Override
public void endElement(String uri,
String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("fruit")) {
System.out.println("End Element :" + qName);
}
}
@Override
public void characters(char[] ch,
int start, int length) throws SAXException {
if (isName) {
System.out.println("Name: "+ new String(ch, start, length));
isName = false;
} else if (isPrice) {
System.out.println("Price: " + new String(ch, start, length));
isPrice = false;
}
}
}
public static void main(String[] args) {
StringBuilder xml = new StringBuilder();
xml.append("<store name='Deliciously Healthy'>")
.append("<fruit>")
.append("<name>Apple</name><price>1.68</price>")
.append("</fruit><fruit>")
.append("<name>Orange</name><price>0.89</price>")
.append("</fruit>")
.append("</store>");
XMLParser xmlParser = new XMLParser();
xmlParser.saxParse(xml.toString());
}
}
The output:
Node name : store; value: Deliciously Healthy
fruit
Name: Apple
Price: 1.68
End Element :fruit
fruit
Name: Orange
Price: 0.89
End Element :fruit
---------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
No comments:
Post a Comment