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

          java Xpath 小例

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

          ????????????
          //?對內(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));

          ????????????
          //?對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>

          主站蜘蛛池模板: 西安市| 岐山县| 昌黎县| 麻栗坡县| 仙桃市| 天镇县| 宝山区| 许昌市| 三门县| 濮阳县| 灵丘县| 巨鹿县| 玉溪市| 南京市| 凤冈县| 抚松县| 新乡县| 东乡族自治县| 古浪县| 鄂州市| 平安县| 上虞市| 民权县| 武功县| 都安| 鲜城| 綦江县| 报价| 东海县| 鹤峰县| 宁夏| 荣昌县| 宁晋县| 邳州市| 绥化市| 宁波市| 芜湖县| 永平县| 林州市| 肃南| 项城市|