Posted on 2007-08-03 15:46
G_G 閱讀(4637)
評(píng)論(0) 編輯 收藏 所屬分類:
xml Related
java
package?test;
import?java.io.StringWriter;
import?javax.xml.parsers.DocumentBuilder;
import?javax.xml.parsers.DocumentBuilderFactory;
import?javax.xml.transform.TransformerFactory;
import?javax.xml.transform.dom.DOMSource;
import?javax.xml.transform.stream.StreamResult;
import?javax.xml.xpath.XPath;
import?javax.xml.xpath.XPathExpression;
import?javax.xml.xpath.XPathFactory;
import?org.w3c.dom.Document;
import?org.w3c.dom.Element;
import?com.opensymphony.xwork2.ActionSupport;
public?class?TAction?extends?ActionSupport?{
????private?String?xml1;
????public?String?getXml1()?{
????????return?xml1;
????}
????public?void?setXml1(String?xml1)?{
????????this.xml1?=?xml1;
????}
????{
????????
????????try?{
????????????DocumentBuilderFactory?factory?=?DocumentBuilderFactory
????????????????????.newInstance();
????????????DocumentBuilder?builder?=?factory.newDocumentBuilder();
????????????//?創(chuàng)建
????????????Document?document?=?builder.newDocument();
????????????Element?root?=?document.createElement("root");
????????????document.appendChild(root);
????????????Element?name?=?document.createElement("name");
????????????name.setAttribute("type",?"ok");
????????????name.appendChild(document.createTextNode("liu!k>a^i!@$^*><{}yi"));
????????????root.appendChild(name);
????????????//?從數(shù)據(jù)庫提取
????????????//?String?xmlstr=?"<beans><name>liu</name><pass>123</pass></beans>";
????????????//?builder.parse(new?ByteArrayInputStream(str.getBytes()));
????????????//?save?to?mysql
????????????StringWriter?output?=?new?StringWriter();
????????????TransformerFactory.newInstance().newTransformer().transform(
????????????????????new?DOMSource(document),?new?StreamResult(output));
????????????//System.out.println(output.toString());
????????????//?對(duì)內(nèi)展現(xiàn)
????????????XPathFactory?factoryXpah?=?XPathFactory.newInstance();
????????????XPath?xpath?=?factoryXpah.newXPath();
????????????XPathExpression?expr?=?xpath.compile("//name");
????????????System.out.println(expr.evaluate(document));
????????????expr?=?xpath.compile("//name/@type");
????????????//System.out.println(expr.evaluate(document));
????????????//?對(duì)jsp?jstl?xml
????????????//?參考?http://www.java2s.com/Code/Java/JSP/XML.htm
????????????this.xml1?=?output.toString().replaceAll("<\\?.*\\?>",?"");
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
????public?static?void?main(String[]?args)?{
????????System.out.println(new?TAction().getXml1());
????}
????
}
jsp
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"?pageEncoding="UTF-8"%>
<%@taglib?uri="http://java.sun.com/jstl/xml_rt"?prefix="x"%>
<%@taglib?uri="http://java.sun.com/jstl/core_rt"?prefix="c"%>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">
</head>
<body>
????<c:import?url="http://127.0.0.1:38080/ljw_svn2/test/data.do"?var="webXml"?/>
????<x:parse?xml="${webXml}"?var="doc"?/>
?????<x:out?select="$doc//name"/><br?/>
</body>
</html>
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"?pageEncoding="UTF-8"%>
<%@taglib?uri="http://java.sun.com/jstl/xml_rt"?prefix="x"%>
<%@taglib?uri="http://java.sun.com/jstl/core_rt"?prefix="c"%>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">
</head>
<body>
????<c:set?var="basepath"?value="<%=request.getScheme()?+?"://"?+?request.getServerName()?+?":"?+?request.getServerPort()?+?request.getContextPath()?+?"/"?%>"></c:set>
????<c:import?url="${basepath}/infoPublishing.do?c=showXmlData&bid=63"?var="webXml"?/>
????<x:parse?xml="${webXml}"?var="doc"?/>
?????類型:<x:out?select="$doc//typeFlag"/><br?/>
?????qq:<x:out?select="$doc//qq"/><br?/>
?????msn:<x:out?select="$doc//msn"/><br?/>
</body>
</html>