怎么解析XML? 大家说说怎么解析XML?(面试题) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 js中解析<html><head><script type="text/javascript"for="window" event="onload">var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")xmlDoc.async="false"xmlDoc.load("xml_note.xml")nodes=xmlDoc.documentElement.childNodesto.innerText= nodes.item(0).textfrom.innerText= nodes.item(1).textheader.innerText=nodes.item(2).textbody.innerText= nodes.item(3).text</script><title>HTML using XML data</title></head><body bgcolor="yellow"><h1>W3Schools.com Internal Note</h1><b>To: </b><span id="to"> </span><br /><b>From: </b><span id="from"></span><hr><b><span id="header"></span></b><hr><span id="body"></span></body></html> java中解析 import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; //下面主要是org.xml.sax包的类 import org.w3c.dom.*; import org.xml.sax.SAXException; public class DOMPrinter{ //打印指定节点(节点名:节点值) public static void printNodeInfo(Node node){ System.out.println(node.getNodeName()+" : "+node.getNodeValue()); } //递归打印全部节点 public static void printNode(Node node){ short nodeType=node.getNodeType(); //判断节点类型 switch(nodeType){ //前缀节点 case Node.PROCESSING_INSTRUCTION_NODE: System.out.println("-----------PI start-----------"); printNodeInfo(node); System.out.println("-----------PI end-----------"); break; //元素节点 case Node.ELEMENT_NODE: System.out.println("-----------Element start-----------"); printNodeInfo(node); System.out.println("-----------Element end-----------"); //打印节点属性值 NamedNodeMap attrs=node.getAttributes(); int attrNum=attrs.getLength(); for(int i=0;i<attrNum;i++){ Node attr=attrs.item(i); System.out.println("-----------Attribute start-----------"); printNodeInfo(attr); System.out.println("-----------Attribute end-----------"); } break; //文本节点 case Node.TEXT_NODE: System.out.println("-----------Text start-----------"); printNodeInfo(node); System.out.println("-----------Text end-----------"); break; default: break; } //递归打印节点信息 Node child=node.getFirstChild(); while(child!=null){ printNode(child); child=child.getNextSibling(); } } public static void main(String[] args){ //DOM解析器的工厂实例 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); try{ //从DOM工厂获得DOM解析器 DocumentBuilder db=dbf.newDocumentBuilder(); //解析XML文档的输入流,得到一个Document Document doc=db.parse(new File("students.xml")); //打印节点 printNode(doc); } catch (ParserConfigurationException e){ e.printStackTrace(); } catch (SAXException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } } } 高分求一功能简单给予Java Web的酒店管理系统源代码 Struts中拦截器采用的什么模式 关于网络上传进来一个XML文件应该怎么把他转化为本地对象进行处理啊? struts相关问题 请达人救啊.........执行TextArea里面的SQL出错了 用Tomcat作服务器,访问Struts页面时出错 困惑几天的问题:Cannot find bean under name org.apache.struts.taglib.html.BEAN 求助!在线等待!! 关于web.xml和struts-config.xml路径问题? 请教高手,访问数据库时出错? Ajax 关于xml的一个问题
<html>
<head>
<script type="text/javascript"
for="window" event="onload">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("xml_note.xml")
nodes=xmlDoc.documentElement.childNodes
to.innerText= nodes.item(0).text
from.innerText= nodes.item(1).text
header.innerText=nodes.item(2).text
body.innerText= nodes.item(3).text
</script>
<title>HTML using XML data</title>
</head>
<body bgcolor="yellow">
<h1>W3Schools.com Internal Note</h1>
<b>To: </b>
<span id="to"> </span>
<br />
<b>From: </b>
<span id="from"></span>
<hr>
<b><span id="header"></span></b>
<hr>
<span id="body"></span>
</body>
</html>
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
//下面主要是org.xml.sax包的类
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class DOMPrinter{
//打印指定节点(节点名:节点值)
public static void printNodeInfo(Node node){
System.out.println(node.getNodeName()+" : "+node.getNodeValue());
}
//递归打印全部节点
public static void printNode(Node node){
short nodeType=node.getNodeType();
//判断节点类型
switch(nodeType){
//前缀节点
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println("-----------PI start-----------");
printNodeInfo(node);
System.out.println("-----------PI end-----------");
break;
//元素节点
case Node.ELEMENT_NODE:
System.out.println("-----------Element start-----------");
printNodeInfo(node);
System.out.println("-----------Element end-----------");
//打印节点属性值
NamedNodeMap attrs=node.getAttributes();
int attrNum=attrs.getLength();
for(int i=0;i<attrNum;i++){
Node attr=attrs.item(i);
System.out.println("-----------Attribute start-----------");
printNodeInfo(attr);
System.out.println("-----------Attribute end-----------");
}
break;
//文本节点
case Node.TEXT_NODE:
System.out.println("-----------Text start-----------");
printNodeInfo(node);
System.out.println("-----------Text end-----------");
break;
default:
break;
}
//递归打印节点信息
Node child=node.getFirstChild();
while(child!=null){
printNode(child);
child=child.getNextSibling();
}
}
public static void main(String[] args){
//DOM解析器的工厂实例
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
//从DOM工厂获得DOM解析器
DocumentBuilder db=dbf.newDocumentBuilder();
//解析XML文档的输入流,得到一个Document
Document doc=db.parse(new File("students.xml"));
//打印节点
printNode(doc);
}
catch (ParserConfigurationException e){
e.printStackTrace();
}
catch (SAXException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
}