>>1.js解析
HTML文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<meta name="author" content="zhaoyong" />
<title></title>
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript">
function loadtb() {
alert("start");
//以下代码经修改,已经兼容IE、FF、Chrome等主流浏览器
//主要增加xmlHttp是为了兼容Chrome
var xmlDoc;
var xmlHttp = null;
var xmlFile = "test.xml";
if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
} else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.load(xmlFile);
} catch(e) {
alert("xmlDoc: " + e);
try {
xmlDoc = document.implementation.createDocument("", "root", null);
xmlDoc = getXml(xmlFile).responseXML;
} catch(e) {
alert(e);
}
}
function getXml(x){
if(xmlHttp != null){
xmlHttp.open("GET",x,false);
xmlHttp.send(null);
} else {
alert("Your browser does not support XMLHTTP.11");
return false;
}
return(xmlHttp);
}
var root = xmlDoc.documentElement;
alert("root.childNodes: " + root.childNodes);
var childs = root.childNodes;
alert("childs.length: " + childs.length);
for(var i=0 ; i<childs.length; i++) {
alert("nodeType: " + root.childNodes(i).nodeType + " nodeName: " + root.childNodes(i).nodeName + " nodeValue: " + root.childNodes(i).nodeValue);
alert(childs(i).firstChild.firstChild.nodeValue);
var bookId = childs[i].getElementsByTagName("bookId")[0].firstChild.nodeValue;
alert(bookId);
//由于FF不支持数组下标用(),所以需要全部替换为[]
var isbn = childs[i].getElementsByTagName("isbn")[0].firstChild.nodeValue;
alert(isbn);
var bookName = childs[i].getElementsByTagName("bookName")[0].firstChild.nodeValue;
alert(bookName);
var pbName = childs[i].getElementsByTagName("pbName")[0].firstChild.nodeValue;
var pbDate = childs[i].getElementsByTagName("pbDate")[0].firstChild.nodeValue;
var price = childs[i].getElementsByTagName("price")[0].firstChild.nodeValue;
var pages = childs[i].getElementsByTagName("pages")[0].firstChild.nodeValue;
var context = childs[i].getElementsByTagName("context")[0].firstChild.nodeValue;
/*
var mytr = document.createElement("tr");
var mytd1 = document.createElement("td");
var mytd2 = document.createElement("td");
var mytd3 = document.createElement("td");
var mytd4 = document.createElement("td");
var mytd5 = document.createElement("td");
var mytd6 = document.createElement("td");
var mytd7 = document.createElement("td");
var mytd8 = document.createElement("td");
var td1_txt = document.createTextNode(bookId);
var td2_txt = document.createTextNode(isbn);
var td3_txt = document.createTextNode(bookName);
var td4_txt = document.createTextNode(pbName);
var td5_txt = document.createTextNode(pbDate);
var td6_txt = document.createTextNode(price);
var td7_txt = document.createTextNode(pages);
var td8_txt = document.createTextNode(context);
mytd1.appendChild(td1_txt);
mytd2.appendChild(td2_txt);
mytd3.appendChild(td3_txt);
mytd4.appendChild(td4_txt);
mytd5.appendChild(td5_txt);
mytd6.appendChild(td6_txt);
mytd7.appendChild(td7_txt);
mytd8.appendChild(td8_txt);
mytr.appendChild(mytd1);
mytr.appendChild(mytd2);
mytr.appendChild(mytd3);
mytr.appendChild(mytd4);
mytr.appendChild(mytd5);
mytr.appendChild(mytd6);
mytr.appendChild(mytd7);
mytr.appendChild(mytd8);
var myta = document.getElementById("ta");
myta.appendChild(mytr);
*/
}
}
function changeAppGroup(thisObj){
//val()获得第一个区配元素的当前值
if($(thisObj).val()==""){
$("#appName").html("<option value='-1'>=请选择=</option>");
}else{
var id = encodeURIComponent($(thisObj).val());
var url = contextName + "/appGrpAction.do?method=getGroupSubApps&appGrpName="+id;
retrieveURL(url,null,null,function(content){
if(content!=null&&content!=""){
var arr = content.split("::");
var childStr = "<option value='-1'>=请选择=</option>";
if(arr.length>1){
for(var i=1;i<arr.length;i++){
childStr += "<option value='" + arr[i] + "'>" + arr[i] + "</option>";
}
}
$("#appName").html(childStr);
return;
}
});
}
}
var j=0;
function change() {
if(j>0) {
return;
}
var content = "1::2::3::4::5";
var arr = content.split("::");
var childStr = "<option value='-1'>=请选择=</option>";
if(arr.length>0){
for(var i=0;i<arr.length;i++){
childStr += "<option value='" + arr[i] + "'>" + arr[i] + "</option>";
}
}
$("#IDCName").html(childStr);
alert("j: " + j);
j++;
return;
}
</script>
</head>
<body>
<table id="ta">
<tr>
<td>
<span>IDC</span>
<select name="IDCName" id="IDCName" οnmοusedοwn="change();">
<option value="AllIDC">
=所有IDC=
</option>
</select>
</td>
</tr>
</table>
<button οnclick="loadtb()">
加载
</button>
</body>
</html>
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<book>
<bookId>bookId1</bookId>
<isbn>isbn1</isbn>
<bookName>bookName1</bookName>
<pbName>pbName1</pbName>
<pbDate>pbDate1</pbDate>
<price>price1</price>
<pages>pages1</pages>
<context>context1</context>
</book>
<book>
<bookId>bookId2</bookId>
<isbn>isbn2</isbn>
<bookName>bookName2</bookName>
<pbName>pbName2</pbName>
<pbDate>pbDate2</pbDate>
<price>price2</price>
<pages>pages2</pages>
<context>context2</context>
</book>
<book>
<bookId>bookId3</bookId>
<isbn>isbn3</isbn>
<bookName>bookName3</bookName>
<pbName>pbName3</pbName>
<pbDate>pbDate3</pbDate>
<price>price3</price>
<pages>pages3</pages>
<context>context3</context>
</book>
</root>
根据路径设置html文件和xml文件必须放在同一目录下。
>>2.java解析
Dom解析:
package com.autonavi.parsexml;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
/**
*
* @author dashan.yin
*
*/
public class XmlParser {
public static void main(String[] args) {
try {
// 解析器工厂类,为创建一个解析器作准备)
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 创建一个dom解析器
// 他实现了解析和处理xml文件需要的全部dom方法
DocumentBuilder db = dbf.newDocumentBuilder();
String url = Thread.currentThread().getContextClassLoader()
.getResource("").toString();
System.out.println("url: " + url);
// 告诉解析器去解析哪个xml文件
Document doc = db.parse(url + "config/idc.xml");
// 如果把整个xml看作一个公司,那么NodeList就好像是一个部门
NodeList nodeList = doc.getElementsByTagName("Idc");
System.out.println("the length=" + nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(doc.getElementsByTagName("IdcId").item(i)
.getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("IdcName").item(i)
.getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("IdcValue").item(i)
.getFirstChild().getNodeValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Idc name="1" value="1">
<IdcId>1</IdcId>
<IdcName>青岛</IdcName>
<IdcValue>IDC_QD</IdcValue>
</Idc>
<Idc>
<IdcId>2</IdcId>
<IdcName>南京</IdcName>
<IdcValue>IDC_NJ</IdcValue>
</Idc>
</root>
根据路径配置,xml文件放在config目录下,如下图所示:
解析复杂xml:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* 本类是专门解析XML文件的,主要用于为系统读取自己的配置文件时提供最方便的解析操作
* @author HX
*
*/
public class XmlManager {
/**
* 得到某节点下某个属性的值
* @param element 要获取属性的节点
* @param attributeName 要取值的属性名称
* @return 要获取的属性的值
* @author HX_2010-01-12
*/
public static String getAttribute( Element element, String attributeName ) {
return element.getAttribute( attributeName );
}
/**
* 获取指定节点下的文本
* @param element 要获取文本的节点
* @return 指定节点下的文本
* @author HX_2010-01-12
*/
public static String getText( Element element ) {
return element.getFirstChild().getNodeValue();
}
/**
* 解析某个xml文件,并在内存中创建DOM树
* @param xmlFile 要解析的XML文件
* @return 解析某个配置文件后的Document
* @throws Exception xml文件不存在
*/
public static Document parse( String xmlFile ) throws Exception {
// 绑定XML文件,建造DOM树
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document domTree = db.parse( xmlFile );
return domTree;
}
/**
* 获得某节点下的某个子节点(指定子节点名称,和某个属性的值)<br>
* 即获取parentElement下名字叫childName,并且属性attributeName的值为attributeValue的子结点
* @param parentElement 要获取子节点的那个父节点
* @param childName 要获取的子节点名称
* @param attributeName 要指定的属性名称
* @param attributeValue 要指定的属性的值
* @return 符合条件的子节点
* @throws Exception 子结点不存在或有多个符合条件的子节点
* @author HX_2008-12-01
*/
public static Element getChildElement( Element parentElement, String childName, String attributeName, String attributeValue ) throws Exception {
NodeList list = parentElement.getElementsByTagName( childName );
int count = 0;
Element curElement = null;
for ( int i = 0 ; i < list.getLength() ; i ++ ) {
Element child = ( Element )list.item( i );
String value = child.getAttribute( attributeName );
if ( true == value.equals( attributeValue ) ) {
curElement = child;
count ++;
}
}
if ( 0 == count ) {
throw new Exception( "找不到个符合条件的子节点!" );
} else if ( 1 < count ) {
throw new Exception( "找到多个符合条件的子节点!" );
}
return curElement;
}
/**
* 得到某节点下的某个子节点(通过指定子节点名称)<br>
* 即获取parentElement下名字叫childName的子节点
* @param parentElement 要获取子节点的父节点
* @param childName 要获取的子节点名称
* @return 符合条件的子节点
* @throws Exception 找不到符合条件的子结点或找到多个符合条件的子节点
*/
public static Element getChildElement( Element parentElement, String childName ) throws Exception {
NodeList list = parentElement.getElementsByTagName( childName );
Element curElement = null;
if ( 1 == list.getLength() ) {
curElement = ( Element )list.item( 0 );
} else if ( 0 == list.getLength() ) {
throw new Exception( "找不到个符合条件的子节点!" );
} else {
throw new Exception( "找到多个符合条件的子节点!" );
}
return curElement;
}
}