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

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

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

    vista

    回顧※展望                     潛心技術(shù)&&不再擱淺

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      12 Posts :: 1 Stories :: 13 Comments :: 0 Trackbacks
    雖然考慮到讀取文件會(huì)影響性能,最終沒有采用這種方式,而是取數(shù)據(jù)庫,但總結(jié)出來以后說不定用的著,呵呵.

    1,下載dom4j
    2,寫xml的代碼
    public?class?AddPictureAction?extends?BaseAction?{
    ????
    public?ActionForward?execute(ActionMapping?mapping,?ActionForm?form,?HttpServletRequest?request,
    ????????????HttpServletResponse?response)?
    throws?DocumentException?{

    ????????
    try?{
    ????????????FileWriter?out?
    =?new?FileWriter(request.getSession().getServletContext().getRealPath(Constants.DOG_SON_HOT_PICTURE));
    ??????????????//這里我用的相對(duì)路徑,代碼里是"WEB-INF//config/a.xml"
    ????????????String?picPath1?
    =?request.getParameter("picPath1");
    ????????????String?picPath2?
    =?request.getParameter("picPath2");
    ????????????String?picPath3?
    =?request.getParameter("picPath3");
    ????????????String?picPath4?
    =?request.getParameter("picPath4");
    ????????????String?picPath5?
    =?request.getParameter("picPath5");
    ????????????String?url1?
    =?request.getParameter("url1");
    ????????????String?url2?
    =?request.getParameter("url2");
    ????????????String?url3?
    =?request.getParameter("url3");
    ????????????String?url4?
    =?request.getParameter("url4");
    ????????????String?url5?
    =?request.getParameter("url5");
    ????????????Document?document?
    =?DocumentHelper.createDocument();
    ????????????Element?root?
    =?document.addElement("root");
    ????????????root.addElement(
    "picPath1").addText(picPath1);
    ????????????root.addElement(
    "picPath2").addText(picPath2);
    ????????????root.addElement(
    "picPath3").addText(picPath3);
    ????????????root.addElement(
    "picPath4").addText(picPath4);
    ????????????root.addElement(
    "picPath5").addText(picPath5);
    ????????????root.addElement(
    "url1").addText(url1);
    ????????????root.addElement(
    "url2").addText(url2);
    ????????????root.addElement(
    "url3").addText(url3);
    ????????????root.addElement(
    "url4").addText(url4);
    ????????????root.addElement(
    "url5").addText(url5);
    ????????????document.write(out);
    ????????????out.close();
    ????????}
    ?catch?(IOException?e)?{
    ????????????e.printStackTrace();
    ????????}

    ????????
    return?mapping.findForward(Constants.SUCCESS);
    ????}


    }

    解析xml的代碼
    ????????Resource?resource?=?new
    ????????????????????FileSystemResource(request.getSession().getServletContext().getRealPath(Constants.DOG_SON_HOT_PICTURE));
    ????????SAXReader?reader?
    =?new?SAXReader();
    ????????Document?document?
    =?reader.read(resource.getInputStream());
    ????????System.out.println(document.asXML());

    dom4j官方入門的文章,我把它拷貝下來了,呵呵:



    Parsing?XML
    One?of?the?first?things?you
    'll?probably?want?to?do?is?to?parse?an?XML?document?of?some?kind.?This?is?easy?to?do?in?dom4j.?The?following?code?demonstrates?how?to?this.?

    import?java.net.URL;

    import?org.dom4j.Document;
    import?org.dom4j.DocumentException;
    import?org.dom4j.io.SAXReader;

    public?class?Foo?{

    ????
    public?Document?parse(URL?url)?throws?DocumentException?{
    ????????SAXReader?reader?
    =?new?SAXReader();
    ????????Document?document?
    =?reader.read(url);
    ????????
    return?document;
    ????}

    }

    Using?Iterators
    A?document?can?be?navigated?using?a?variety?of?methods?that?
    return?standard?Java?Iterators.?For?example?

    ????
    public?void?bar(Document?document)?throws?DocumentException?{

    ????????Element?root?
    =?document.getRootElement();

    ????????
    //?iterate?through?child?elements?of?root
    ????????for?(?Iterator?i?=?root.elementIterator();?i.hasNext();?)?{
    ????????????Element?element?
    =?(Element)?i.next();
    ????????????
    //?do?something
    ????????}


    ????????
    //?iterate?through?child?elements?of?root?with?element?name?"foo"
    ????????for?(?Iterator?i?=?root.elementIterator(?"foo"?);?i.hasNext();?)?{
    ????????????Element?foo?
    =?(Element)?i.next();
    ????????????
    //?do?something
    ????????}


    ????????
    //?iterate?through?attributes?of?root?
    ????????for?(?Iterator?i?=?root.attributeIterator();?i.hasNext();?)?{
    ????????????Attribute?attribute?
    =?(Attribute)?i.next();
    ????????????
    //?do?something
    ????????}

    ?????}

    Powerful?Navigation?with?XPath
    In?dom4j?XPath?expressions?can?be?evaluated?on?the?Document?or?on?any?Node?in?the?tree?(such?as?Attribute,?Element?or?ProcessingInstruction).?This?allows?complex?navigation?throughout?the?document?with?a?single?line?of?code.?For?example.?

    ????
    public?void?bar(Document?document)?{
    ????????List?list?
    =?document.selectNodes(?"//foo/bar"?);

    ????????Node?node?
    =?document.selectSingleNode(?"//foo/bar/author"?);

    ????????String?name?
    =?node.valueOf(?"@name"?);
    ????}

    For?example?
    if?you?wish?to?find?all?the?hypertext?links?in?an?XHTML?document?the?following?code?would?do?the?trick.?

    ????
    public?void?findLinks(Document?document)?throws?DocumentException?{

    ????????List?list?
    =?document.selectNodes(?"//a/@href"?);

    ????????
    for?(Iterator?iter?=?list.iterator();?iter.hasNext();?)?{
    ????????????Attribute?attribute?
    =?(Attribute)?iter.next();
    ????????????String?url?
    =?attribute.getValue();
    ????????}

    ????}

    If?you?need?any?help?learning?the?XPath?language?we?highly?recommend?the?Zvon?tutorial?which?allows?you?to?learn?by?example.?

    Fast?Looping
    If?you?ever?have?to?walk?a?large?XML?document?tree?then?
    for?performance?we?recommend?you?use?the?fast?looping?method?which?avoids?the?cost?of?creating?an?Iterator?object?for?each?loop.?For?example?

    ????
    public?void?treeWalk(Document?document)?{
    ????????treeWalk(?document.getRootElement()?);
    ????}


    ????
    public?void?treeWalk(Element?element)?{
    ????????
    for?(?int?i?=?0,?size?=?element.nodeCount();?i?<?size;?i++?)?{
    ????????????Node?node?
    =?element.node(i);
    ????????????
    if?(?node?instanceof?Element?)?{
    ????????????????treeWalk(?(Element)?node?);
    ????????????}

    ????????????
    else?{
    ????????????????
    //?do?something.
    ????????????}

    ????????}

    ????}

    Creating?a?
    new?XML?document
    Often?in?dom4j?you?will?need?to?create?a?
    new?document?from?scratch.?Here's?an?example?of?doing?that.?

    import?org.dom4j.Document;
    import?org.dom4j.DocumentHelper;
    import?org.dom4j.Element;

    public?class?Foo?{

    ????
    public?Document?createDocument()?{
    ????????Document?document?
    =?DocumentHelper.createDocument();
    ????????Element?root?
    =?document.addElement(?"root"?);

    ????????Element?author1?
    =?root.addElement(?"author"?)
    ????????????.addAttribute(?
    "name",?"James"?)
    ????????????.addAttribute(?
    "location",?"UK"?)
    ????????????.addText(?
    "James?Strachan"?);
    ????????
    ????????Element?author2?
    =?root.addElement(?"author"?)
    ????????????.addAttribute(?
    "name",?"Bob"?)
    ????????????.addAttribute(?
    "location",?"US"?)
    ????????????.addText(?
    "Bob?McWhirter"?);

    ????????
    return?document;
    ????}

    }

    Writing?a?document?to?a?file
    A?quick?and?easy?way?to?write?a?Document?(or?any?Node)?to?a?Writer?is?via?the?write()?method.?

    ??FileWriter?out?
    =?new?FileWriter(?"foo.xml"?);
    ??document.write(?out?);
    If?you?want?to?be?able?to?change?the?format?of?the?output,?such?as?pretty?printing?or?a?compact?format,?or?you?want?to?be?able?to?work?with?Writer?objects?or?OutputStream?objects?as?the?destination,?then?you?can?use?the?XMLWriter?
    class.?

    import?org.dom4j.Document;
    import?org.dom4j.io.OutputFormat;
    import?org.dom4j.io.XMLWriter;

    public?class?Foo?{

    ????
    public?void?write(Document?document)?throws?IOException?{

    ????????
    //?lets?write?to?a?file
    ????????XMLWriter?writer?=?new?XMLWriter(
    ????????????
    new?FileWriter(?"output.xml"?)
    ????????);
    ????????writer.write(?document?);
    ????????writer.close();


    ????????
    //?Pretty?print?the?document?to?System.out
    ????????OutputFormat?format?=?OutputFormat.createPrettyPrint();
    ????????writer?
    =?new?XMLWriter(?System.out,?format?);
    ????????writer.write(?document?);

    ????????
    //?Compact?format?to?System.out
    ????????format?=?OutputFormat.createCompactFormat();
    ????????writer?
    =?new?XMLWriter(?System.out,?format?);
    ????????writer.write(?document?);
    ????}

    }

    Converting?to?and?from?Strings
    If?you?have?a?reference?to?a?Document?or?any?other?Node?such?as?an?Attribute?or?Element,?you?can?turn?it?into?the?
    default?XML?text?via?the?asXML()?method.?

    ????????Document?document?
    =?;
    ????????String?text?
    =?document.asXML();
    If?you?have?some?XML?as?a?String?you?can?parse?it?back?into?a?Document?again?using?the?helper?method?DocumentHelper.parseText()?

    ????????String?text?
    =?"<person>?<name>James</name>?</person>";
    ????????Document?document?
    =?DocumentHelper.parseText(text);
    Styling?a?Document?with?XSLT
    Applying?XSLT?on?a?Document?is?quite?straightforward?using?the?JAXP?API?from?Sun.?This?allows?you?to?work?against?any?XSLT?engine?such?as?Xalan?or?SAXON.?Here?is?an?example?of?using?JAXP?to?create?a?transformer?and?then?applying?it?to?a?Document.?

    import?javax.xml.transform.Transformer;
    import?javax.xml.transform.TransformerFactory;

    import?org.dom4j.Document;
    import?org.dom4j.io.DocumentResult;
    import?org.dom4j.io.DocumentSource;

    public?class?Foo?{

    ????
    public?Document?styleDocument(
    ????????Document?document,?
    ????????String?stylesheet
    ????)?
    throws?Exception?{

    ????????
    //?load?the?transformer?using?JAXP
    ????????TransformerFactory?factory?=?TransformerFactory.newInstance();
    ????????Transformer?transformer?
    =?factory.newTransformer(?
    ????????????
    new?StreamSource(?stylesheet?)?
    ????????);

    ????????
    //?now?lets?style?the?given?document
    ????????DocumentSource?source?=?new?DocumentSource(?document?);
    ????????DocumentResult?result?
    =?new?DocumentResult();
    ????????transformer.transform(?source,?result?);

    ????????
    //?return?the?transformed?document
    ????????Document?transformedDoc?=?result.getDocument();
    ????????
    return?transformedDoc;
    ????}

    posted on 2006-10-31 17:10 Vista 閱讀(1446) 評(píng)論(0)  編輯  收藏 所屬分類: java基礎(chǔ)

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 三年片在线观看免费观看大全中国| 自拍偷自拍亚洲精品第1页 | 成在人线av无码免费高潮喷水 | 1000部无遮挡拍拍拍免费视频观看 | 亚洲av永久无码精品国产精品| 一级毛片a女人刺激视频免费 | 免费无码又爽又刺激高潮软件| 亚洲一区无码精品色| 成人免费网站久久久| 又黄又大又爽免费视频| 美女被艹免费视频| 亚洲精品乱码久久久久久不卡| 亚洲精品视频免费 | 亚洲一卡2卡3卡4卡国产网站| 亚洲免费福利视频| 久久久久久亚洲精品影院| 午夜视频免费观看| 免费国产黄网站在线看| AV在线亚洲男人的天堂| 国产一精品一AV一免费| 亚洲成人高清在线观看| 永久中文字幕免费视频网站| 添bbb免费观看高清视频| 亚洲午夜久久久久久久久电影网| 18禁超污无遮挡无码免费网站 | 全免费a级毛片免费**视频| 亚洲αⅴ无码乱码在线观看性色 | 国产中文字幕免费| 中文字幕成人免费高清在线| 色噜噜综合亚洲av中文无码| 一区二区无码免费视频网站 | 国产免费拔擦拔擦8X高清在线人| 亚洲一区二区三区电影| 成人毛片18女人毛片免费| 九九全国免费视频| 亚洲大片免费观看| 亚洲男人第一无码aⅴ网站| 免费无遮挡无码永久视频| 亚洲啪AV永久无码精品放毛片| 中文亚洲AV片在线观看不卡| 美女裸身网站免费看免费网站|