<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    java Xpath 小例

    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>

    主站蜘蛛池模板: 亚洲综合一区二区精品久久| 男女交性无遮挡免费视频| 最近最新中文字幕完整版免费高清| 亚洲国产精品无码久久98 | a毛片成人免费全部播放| 亚洲免费在线播放| 国内自产拍自a免费毛片| 最近免费mv在线观看动漫| 亚洲综合av一区二区三区不卡 | 亚洲天堂免费在线视频| 亚洲无线一二三四区| 亚洲国产精品自产在线播放| 91短视频在线免费观看| 麻豆91免费视频| 91嫩草亚洲精品| 亚洲日韩激情无码一区| 午夜高清免费在线观看| 免费无码毛片一区二区APP| 国产精品亚洲专区无码不卡| 亚洲色图校园春色| 中文字幕亚洲不卡在线亚瑟| 日本一区二区三区免费高清 | www.亚洲色图.com| 国产福利在线免费| 久久99热精品免费观看牛牛| 免费VA在线观看无码| 亚洲日韩精品无码专区加勒比☆ | 亚洲免费福利在线视频| 亚洲成AV人片在线观看无| 国产gav成人免费播放视频| 免费观看黄色的网站| 暖暖在线视频免费视频| 色婷婷综合缴情综免费观看 | 免费人成毛片动漫在线播放| 免费的黄网站男人的天堂| 久久亚洲国产最新网站| 亚洲视频一区在线播放| 亚洲AV无码国产精品麻豆天美| 亚洲精品线路一在线观看| 国产亚洲福利一区二区免费看| 成人无遮挡裸免费视频在线观看|