對于XML這塊內(nèi)容的要求:
- 能使用jaxp來處理xml的讀操作
- 理解dom與sax的區(qū)別
- 熟練掌握jdom或dom4j創(chuàng)建xml的方式并知道如何在javaweb項目中返回xml
- 熟練掌握jdom或dom4j解析xml的方式
- 掌握觀察者模式
- 了解dtd和schema,能通過dtd和schema寫出有效的xml
- 了解jquery解析xml的方式
預備知識點:
- dom:document object model 文檔對象模型
- sax:simple apis for xml xml解析的簡單api
面試題目一:dom與sax解析xml的區(qū)別是什么?
答案:dom會將整個xml讀入到內(nèi)存后,為我們創(chuàng)建一個文檔對象击碗,之后讀寫數(shù)據(jù)都是對其進行操作,這種方式比較占據(jù)內(nèi)存胆剧,好處是有了文檔對象后可以隨意讀寫稻艰,而sax是基于事件的鸭栖,將xml讀入內(nèi)存的過程中晶丘,當發(fā)生相應的事件時浮梢,觸發(fā)相應的動作跛十,只存儲當前讀到的內(nèi)容,之前讀取的內(nèi)容會丟失(覆蓋)秕硝,底層使用觀察者模式芥映,因為沒有對之前的數(shù)據(jù)進行記憶所以只能對xml進行讀操作
本質(zhì):dom讀完xml之后再處理,而sax邊讀邊處理远豺。
面試題目二:使用jaxp中的DomcumentBuilderFactory來解析如下xml
<?xml version="1.0"?>
<PEOPLE>
<PERSON PERSONID="E01">
<NAME>Tony Blair</NAME>
<ADDRESS>10 Dowing Street , London, UK </ADDRESS>
<TEL>(061) 98765 </TEL>
<FAX>(061) 98765 </FAX>
<EMAIL>blair@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E02">
<NAME>Bill Clinton</NAME>
<ADDRESS>White House, USA</ADDRESS>
<TEL>(001) 6400 98765</TEL>
<FAX>(001) 6400 98765</FAX>
<EMAIL>bill@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E03">
<NAME>Tom Cruise</NAME>
<ADDRESS>57 Jumbo Street, New York, USA</ADDRESS>
<TEL>(001) 4500 67859</TEL>
<FAX>(001) 4500 67859</FAX>
<EMAIL>cruise@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E04">
<NAME>Linda Goodman</NAME>
<ADDRESS>78 cRAX lANE, lONDON, UK</ADDRESS>
<TEL>(061) 54 56789</TEL>
<FAX>(061) 54 56789</FAX>
<EMAIL>linda@everywhere.com</EMAIL>
</PERSON>
</PEOPLE>
答案:具體代碼如下:
package com.test;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
//使用jaxp解析xml
public class XMLParseTest {
public static void main(String[] args) throws Exception {
// step 1: 獲得解析器工廠
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// step 2: 通過工廠來獲得具體的解析器
DocumentBuilder db = dbf.newDocumentBuilder();
// step 3: 通過解析器來對xml處理
Document doc = db.parse(new File("people.xml"));
// 目標:打印出每個person的id值奈偏、name值、address值躯护、tel值惊来、fax值、email值
NodeList persons = doc.getElementsByTagName("PERSON");
for (int i = 0; persons != null && i < persons.getLength(); i++) {
Element person = (Element) persons.item(i);
// 獲得person元素的屬性personid
String personId = person.getAttribute("PERSONID");
String name = person.getElementsByTagName("NAME").item(0)
.getFirstChild().getNodeValue();
String address = person.getElementsByTagName("ADDRESS").item(0)
.getFirstChild().getNodeValue();
String tel = person.getElementsByTagName("TEL").item(0)
.getFirstChild().getNodeValue();
String fax = person.getElementsByTagName("FAX").item(0)
.getFirstChild().getNodeValue();
String email = person.getElementsByTagName("EMAIL").item(0)
.getFirstChild().getNodeValue();
System.out.println("PERSONID:" + personId);
System.out.println("NAME:" + name);
System.out.println("ADDRESS:" + address);
System.out.println("TEL:" + tel);
System.out.println("FAX:" + fax);
System.out.println("EMAIL:" + email);
System.out.println("---------------------");
}
}
}
面試題目三:使用jaxp中的SAXParserFactory來解析上述的xml
代碼如下:
public class XMLParserBySAXTest {
public static void main(String[] args) throws Exception {
//step 1: 獲得SAX工廠
SAXParserFactory factory = SAXParserFactory.newInstance();
//step 2: 獲得SAX解析器
SAXParser newSAXParser = factory.newSAXParser();
//生成一個具體觀察者對象棺滞,類似于awt中的監(jiān)聽器
MyDefaultHandler myDefaultHandler = new MyDefaultHandler();
//step 3: 進行解析
newSAXParser.parse(new File("people.xml"), myDefaultHandler);
List<People> list = myDefaultHandler.getList();
for (People people : list) {
Map attribute = people.getAttribute();
for (Object key: attribute.keySet()) {
System.out.println(key + "=\"" + attribute.get(key) + "\"");
}
System.out.println("NAME: " + people.getName());
System.out.println("ADDRESS: " + people.getAddress());
System.out.println("Tel: " + people.getTel());
System.out.println("Fax: " + people.getFax());
System.out.println("Email: " + people.getEmail());
}
}
}
//讀取xml并封裝成對應的model集合
class MyDefaultHandler extends DefaultHandler {
List<People> list = new ArrayList<People>();
People people = null;
String qName = null;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (("PERSON").equalsIgnoreCase(qName)) {
people = new People();
if (null != attributes) {
Map<String, String> map = null;
for (int i = 0; i < attributes.getLength(); i++) {
map = new HashMap<String, String>();
map.put(attributes.getQName(i), attributes.getValue(i));
}
people.setAttribute(map);
}
}
this.qName = qName;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (("PERSON").equalsIgnoreCase(qName)) {
list.add(people);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String content = new String(ch, start, length);
if (!StringUtils.isBlank(content)) {
if (("NAME").equalsIgnoreCase(qName)) {
people.setName(content);
} else if (("ADDRESS").equalsIgnoreCase(qName)) {
people.setAddress(content);
} else if (("TEL").equalsIgnoreCase(qName)) {
people.setTel(content);
} else if (("FAX").equalsIgnoreCase(qName)) {
people.setFax(content);
} else if (("EMAIL").equalsIgnoreCase(qName)) {
people.setEmail(content);
}
}
}
public List<People> getList() {
return list;
}
}
總結(jié):個人感覺用jaxp的sax處理xml對于開發(fā)者而言還是比較麻煩的裁蚁,第一點是很少用這個接口來處理xml操作起來還要去查看api文檔,第二點是要寫很多if继准,第三點是該api中的幾個方法之間如何協(xié)作要事先搞清楚枉证。
面試題目四:使用jdom方式創(chuàng)建并生成xml文件,并說說jdom與dom的區(qū)別
答案:使用jdom創(chuàng)建xml文件的代碼如下所示
public class JDOMTest {
public static void main(String[] args) throws IOException {
Element root = new Element("hello").setAttribute("name", "zhangsan")
.setAttribute("age", "20").setAttribute("sex", "male");
root.addContent(new Comment("This is a comment"));
root.addContent("helloworld");
root.addContent(new Element("world").setAttribute("address", "China")
.addContent("This is another element"));
Document doc = new Document(root);
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
output.output(doc, new FileWriter("test.xml"));
}
}
dom是w3c制定的一種IDL語言(接口描述語言)移必,為了兼容各種語言室谚,它把標準的要求降的很低,java開發(fā)人員用起來很麻煩崔泵,而Jdom是用純java開發(fā)的秒赤,java開發(fā)成員用起來就非常順手,并且它用來處理xml更加高效管削。
面試題目五:請用jdom讀取如下xml
?xml version="1.0"?>
<PEOPLE>
<PERSON PERSONID="E01">
<NAME>Tony Blair</NAME>
<ADDRESS>10 Dowing Street , London, UK </ADDRESS>
<TEL>(061) 98765 </TEL>
<FAX>(061) 98765 </FAX>
<EMAIL>blair@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E02">
<NAME>Bill Clinton</NAME>
<ADDRESS>White House, USA</ADDRESS>
<TEL>(001) 6400 98765</TEL>
<FAX>(001) 6400 98765</FAX>
<EMAIL>bill@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E03">
<NAME>Tom Cruise</NAME>
<ADDRESS>57 Jumbo Street, New York, USA</ADDRESS>
<TEL>(001) 4500 67859</TEL>
<FAX>(001) 4500 67859</FAX>
<EMAIL>cruise@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E04">
<NAME>Linda Goodman</NAME>
<ADDRESS>78 cRAX lANE, lONDON, UK</ADDRESS>
<TEL>(061) 54 56789</TEL>
<FAX>(061) 54 56789</FAX>
<EMAIL>linda@everywhere.com</EMAIL>
</PERSON>
</PEOPLE>
答案如下:
public class JDomReaderTest {
public static void main(String[] args) throws Exception {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File("people.xml"));
Element root = doc.getRootElement();
System.out.println("根元素為:" + root.getName());
List<Element> person = root.getChildren();
for (Element people : person) {
Attribute personid = people.getAttribute("PERSONID");
String name = people.getChild("NAME").getValue();
String address = people.getChild("ADDRESS").getValue();
String tel = people.getChild("TEL").getValue();
String fax = people.getChild("FAX").getValue();
String email = people.getChild("EMAIL").getValue();
System.out.println("personid:" + personid.getValue());
System.out.println("name:" + name);
System.out.println("address:" + address);
System.out.println("TEL:" + tel);
System.out.println("fax:" + fax);
System.out.println("email:" + email);
System.out.println("-------------------------");
}
}
}
總結(jié):jdom主要用到org.jdom包下的類(Element倒脓、Attribute、Comment 等等)含思、org.jdom.input包下的SAXBuilder(讀取速度比DOMBuilder快)以及org.jdom.output下的XMLOutputter類