posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          java Xpath 小例

          Posted on 2007-08-03 15:46 G_G 閱讀(4640) 評論(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>

          主站蜘蛛池模板: 平乐县| 星子县| 专栏| 阳信县| 屏东市| 云和县| 中牟县| 淮滨县| 湛江市| 百色市| 上高县| 海安县| 永昌县| 宁明县| 莆田市| 安西县| 上高县| 天津市| 嘉鱼县| 曲阳县| 新田县| 杂多县| 邹城市| 唐山市| 龙泉市| 镇宁| 厦门市| 兴业县| 喜德县| 微博| 固阳县| 安阳县| 象山县| 万安县| 广德县| 安泽县| 新营市| 紫金县| 福建省| 上杭县| 肃宁县|