Posted on 2007-08-03 15:46
G_G 阅读(4635)
评论(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();
// 创建
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);
// 从数据库提取
// 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());
// 对内展现
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));
// 对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>