备注:使用java原生dom解析
1.java代码
package JDBC;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
String xmlPath = "D:" + File.separator + "XML.xml";// 得到XML文件地址
File file = new File(xmlPath);// 得到xml文件
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// 得到工厂
DocumentBuilder docb = factory.newDocumentBuilder();// 得到DOC建造者
Document doc = docb.parse(file);// 将文件转换成文档
/*
* 根据Id得到对应的元素中的内容,使用getElementById必须有加载对应的dtd文件
*/
System.out.println(doc.getElementById("1").getTextContent());
// // 得到xml的编码集
// System.out.println(doc.getXmlEncoding());
// // 得到版本号
// System.out.println(doc.getXmlVersion());
//
// // 得到第一个节点的名字
// System.out.println(doc.getFirstChild().getNodeName());
}
}
2.XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (select,insert,update,delete)>
<!ELEMENT insert (#PCDATA)>
<!ATTLIST insert id ID "0">
<!ELEMENT select (#PCDATA)>
<!ATTLIST select id ID "0" >
<!ELEMENT update (#PCDATA)>
<!ATTLIST update id ID "0">
<!ELEMENT delect (#PCDATA)>
<!ATTLIST delect id ID "0">
]>
<note>
<select id="1">
slect * from table order by desc1
</select>
<insert id="2">
slect * from table order by desc2
</insert>
<update id="3">
slect * from table order by desc3
</update>
<delete id="4">
slect * from table order by desc4
</delete>
</note>