Java ???? XML ????????????????????
???????????? ???????[ 2012/8/15 14:31:03 ] ????????
????????????????У?DOMParser ?? Parse() ??????????? XML ????????????? DOM Document ???????? DocumentBuilderFactory ???????? DOM ??????????????? XML ????? ?????? XML ???????? Document ????????????????????е? API ?????????????????е??????з???????? ???????????? Element ????? getChildNodes() ??????????????????е????????а????????????????????? Element ??????????????ж??
??????????? DOM ???? XML ???????????????????????????? XML ????? DOM ?????????????????? API ??????з???????????????????????? ???? DOM ???? XML ?????????? XML ????????????????????????????У?????????? DOM ???????? XML ?????
????SAX ???? XML
?????? DOM ???????ν??????????SAX ???????????????? XML ?????????? XML ??????????????????????????? ???? SAX ????? XML ?????????????????????????????? DOM ?????????????????????????????????? ?? SAX ?? XML ?????????????????????????κ??????????????????????е???????
????????????? SAX ???? XML ???????
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SAXParser {
class BookHandler extends DefaultHandler {
private List<String> nameList;
private boolean title = false;
public List<String> getNameList() {
return nameList;
}
// Called at start of an XML document
@Override
public void startDocument() throws SAXException {
System.out.println("Start parsing document...");
nameList = new ArrayList<String>();
}
// Called at end of an XML document
@Override
public void endDocument() throws SAXException {
System.out.println("End");
}
/**
* Start processing of an element.
* @param namespaceURI Namespace URI
* @param localName The local name?? without prefix
* @param qName The qualified name?? with prefix
* @param atts The attributes of the element
*/
@Override
public void startElement(String uri?? String localName?? String qName??
Attributes atts) throws SAXException {
// Using qualified name because we are not using xmlns prefixes here.
if (qName.equals("title")) {
title = true;
}
}
@Override
public void endElement(String namespaceURI?? String localName?? String qName)
throws SAXException {
// End of processing current element
if (title) {
title = false;
}
}
@Override
public void characters(char[] ch?? int start?? int length) {
// Processing character data inside an element
if (title) {
String bookTitle = new String(ch?? start?? length);
System.out.println("Book title: " + bookTitle);
nameList.add(bookTitle);
}
}
}
public static void main(String[] args) throws SAXException?? IOException {
XMLReader parser = XMLReaderFactory.createXMLReader();
BookHandler bookHandler = (new SAXParser()).new BookHandler();
parser.setContentHandler(bookHandler);
parser.parse("books.xml");
System.out.println(bookHandler.getNameList());
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11