java面试XML相关
对于XML这块内容的要求:
- 能使用jaxp来处理xml的读操作
- 理解dom与sax的区别
- 熟练掌握jdom或dom4j创建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的区别是什么?
答案:dom会将整个xml读入到内存后,为我们创建一个文档对象,之后读写数据都是对其进行操作,这种方式比较占据内存,好处是有了文档对象后可以随意读写,而sax是基于事件的,将xml读入内存的过程中,当发生相应的事件时,触发相应的动作,只存储当前读到的内容,之前读取的内容会丢失(覆盖),底层使用观察者模式,因为没有对之前的数据进行记忆所以只能对xml进行读操作
本质: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中的监听器
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;
}
}
总结:个人感觉用jaxp的sax处理xml对于开发者而言还是比较麻烦的,第一点是很少用这个接口来处理xml操作起来还要去查看api文档,第二点是要写很多if,第三点是该api中的几个方法之间如何协作要事先搞清楚。
面试题目四:使用jdom方式创建并生成xml文件,并说说jdom与dom的区别
答案:使用jdom创建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开发人员用起来很麻烦,而Jdom是用纯java开发的,java开发成员用起来就非常顺手,并且它用来处理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("-------------------------");
}
}
}
总结:jdom主要用到org.jdom包下的类(Element、Attribute、Comment 等等)、org.jdom.input包下的SAXBuilder(读取速度比DOMBuilder快)以及org.jdom.output下的XMLOutputter类