JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java & XML、Web Service  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 想問一下JDOM針對XML&XSLT的處理 [精華]
flashyang

我是巨嘴鳥咕咕~~^Q^



發文: 16
積分: 1
於 2003-07-16 22:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
想請問各位大大如何針對一個XML檔來配合一個XSLT輸出成一個HTML檔....
我是去找過大陸的solution..可是我試就是出不來...能不能請大大提供個範例或網站資訊Big Smile...謝謝...


reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2003-07-16 22:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
應該不難我還沒試過
但是你把
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();  
Source xslSource = new StreamSource(new File("webapps/examples/jsp/Books.xsl"));
Transformer serializer = tf.newTransformer(xslSource);
serializer.transform(new JDOMSource(妳的Document), out);
serializer.setOutputProperty(OutputKeys.ENCODING,"Big5");
serializer.transform(domSource, streamResult);

這樣應該就可以???
try這樣可不可以

koji


reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
ray_linn

什么都不懂的小白

版主

發文: 540
積分: 10
於 2003-07-17 12:49 user profilesend a private message to usersend email to ray_linnreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
JDOM 可以與 Apache 的 Xalan 産品結合在一起。
首先是用戶的汽車的資訊。
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<car vin="123fhg5869705iop90">
  <!--Description of a car-->
  <make>Toyota</make>
  <model>Celica</model>
  <year>1997</year>
  <color>green</color>
  <license state="CA">1ABC234</license>
</car>

下一步,我們將把這個 JDOM Document 與一個 XSL 樣式表結合起來並把 HTML 格式的結果輸出到 servlet 的 OutputStream 上以便在用戶的瀏覽器中顯示。car.xsl:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/car">
    <html>
        <head>
          <title><xsl:value-of select="make"/> <xsl:value-of select="model"/>
        </head>
        <body>
          <h1><xsl:value-of select="make"/></h1><br />
          <h2><xsl:value-of select="model"/></h2><br />
          <table border="0">
          <tr><td>VIN:</td><td><xsl:value-of select="@vin"/></td></tr>
          <tr><td>Year:</td><td><xsl:value-of select="year"/></td></tr>
          <tr><td>Color:</td><td><xsl:value-of select="color"/></td></tr>
          </table>
        </body>
    </html>
  </xsl:template>
</xsl:stylesheet>


現在我們將把 org.jdom.Document 轉換爲 DOM Document,並將其與顯示我們的 XSL 和 OutputStream 的文件一起提供給 Xalan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
 
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
 
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
 
public class ExcXSLT
{
      public static Document readDocument() {
        try {
            SAXBuilder builder = new SAXBuilder();
            Document anotherDocument = builder.build(new File(".\\car.xml"));
            return anotherDocument;
        } catch(JDOMException e) {
            e.printStackTrace();
        } catch(NullPointerException e) {
            e.printStackTrace();
        } catch(java.io.IOException e) {
          e.printStackTrace();
        }
        
        return null;
    }
  public static void executeXSL(Document myDocument) {
    try {
      TransformerFactory tFactory = TransformerFactory.newInstance();
            // Make the input sources for the XML and XSLT documents
            org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter();
            org.w3c.dom.Document domDocument = outputter.output(myDocument);
            javax.xml.transform.Source xmlSource = new javax.xml.transform.dom.DOMSource(domDocument);
            StreamSource xsltSource = new StreamSource(new FileInputStream(".\\car.xsl"));
      //Make the output result for the finished document
       //StreamResult xmlResult = new StreamResult(response.getOutputStream());
            StreamResult xmlResult = new StreamResult(System.out);
      //Get a XSLT transformer
      Transformer transformer = tFactory.newTransformer(xsltSource);
      //do the transform
      transformer.transform(xmlSource, xmlResult);
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(TransformerConfigurationException e) {
            e.printStackTrace();
    } catch(TransformerException e) {
            e.printStackTrace();
        } catch(org.jdom.JDOMException e) {
            e.printStackTrace();
        }
  }
     public static void main(String argv[]) {
        executeXSL(readDocument());
    }
}


ray_linn edited on 2003-07-17 15:42
reply to postreply to post
飞翔的候鸟
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
flashyang

我是巨嘴鳥咕咕~~^Q^



發文: 16
積分: 1
於 2003-07-17 13:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝大大的幫忙..可是我試了後還是會出現下面錯誤訊息....
===============================
org.jdom.JDOMException: Exception outputting Document: Reflection failed while creating new JAXP document: org/w3c/dom/ranges/DocumentRangeerror
===============================

Q___Q我為什麼一直出現這個Exception......我是直接用Exception來接收所有Exception來處理....似乎一直錯在下面這句...

org.w3c.dom.Document domDocument = outputter.output(myDocument);


reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
ray_linn

什么都不懂的小白

版主

發文: 540
積分: 10
於 2003-07-17 15:30 user profilesend a private message to usersend email to ray_linnreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
現在改成完整的代碼叻,環境java 1.4.x+jdom b9
這?是我的輸齣
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ToyotaCelica</title>
</head>
<body>
<h1>Toyota</h1>
<br>
<h2>Celica</h2>
<br>
<table border="0">
<tr>
<td>VIN:</td><td>123fhg5869705iop90</td>
</tr>
<tr>
<td>Year:</td><td>1997</td>
</tr>
<tr>
<td>Color:</td><td>green</td>
</tr>
</table>
</body>
</html>

再不齣來,我就沒理法度樂。。。


reply to postreply to post
飞翔的候鸟
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
flashyang

我是巨嘴鳥咕咕~~^Q^



發文: 16
積分: 1
於 2003-07-17 15:57 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝大大..我會努力狂試的.Big Smile.

reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
flashyang

我是巨嘴鳥咕咕~~^Q^



發文: 16
積分: 1
於 2003-07-18 11:43 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
由於我在這裡發生了許多問題....像是透過DOMOutputter來取得
Document會有錯誤等等...在這裡給大家另一個好的寫法~~
//========================

//1.先建立SAXBuilder物件來建製Document~~
SAXBuilder builder = new SAXBuilder();
Document xmlDocuemnt= builder.build(new File("oxooxox.xml"));

//========================

//2.再透過TransformerFactory製出實體物件..
//並且透過此一物件來處理XSLT Soruce檔
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource("oxoxox.xsl"));

//========================

//3.使用JDOMSource物件將代表XML的Document物件來做為參數

JDOMSource source = new JDOMSource(xmlDocuemnt);

//========================

//4.使用Transformer將此source做轉換
//(根據transformer所代表的XSLT)
//並將輸出結果給JDOMResult 物件

JDOMResult result = new JDOMResult();
transformer.transform(source, result);
      
//========================

//5.用XMLOutputter物件做輸出動作
//針對JDOMResult物件來取得Document物件再做輸出

XMLOutputter outputter = new XMLOutputter();
outputter.setEncoding("big5"); //記得要設Big5才能看中文
outputter.setNewLines(true); //一定要設..不然檔案不會分行
outputter.output(result.getDocument(),
new FileOutputStream("oxooxox.htm")
);


flashyang edited on 2003-07-18 12:28
reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
ray_linn

什么都不懂的小白

版主

發文: 540
積分: 10
於 2003-07-18 13:35 user profilesend a private message to usersend email to ray_linnreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System.IO;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace cc.ejb
{
    class XML2HTML
    {
       [STAThread]
       static void main(string[] args)
         {
            XPathDocument doc=new XPathDocument(@".\car.xml");
            XslTransform trans=new XslTransform();
            trans.Load(@".\car.xsl);
            XPathNavigator nav=((IXPathNavigable)doc).CreateNavigator();
            FileStream fs=new FileStream(@".\car.html");
             trans.Transform(nav,null,fs);
        }
   }
}

有喜欢C#的可以看一下。


reply to postreply to post
飞翔的候鸟
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
garbage

以死美工自居



發文: 65
積分: 0
於 2003-07-23 09:10 user profilesend a private message to usersend email to garbagereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
老揚,不知道你當時後有這問題的說
我這邊有相關的資料...
不過既然你都解決了...那就省啦...呵呵...


reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
flashyang

我是巨嘴鳥咕咕~~^Q^



發文: 16
積分: 1
於 2003-08-01 14:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你好樣的...有寶不會給..Q___Q嗚....anyway....我的文件轉換系統已經完成了....不過只允許公司內部特定文件....我PO一下我的東西好了..Big Smile
----------------------------------------------------------------------------------------------------
下載點:
http://home.pchome.com.tw/young/flashyang/DocConsolidation_v1.0.zip

目的:文件轉換系統

動機:公司內部文件必須要抓取特定資訊來另成新檔...這些檔是要用來翻釋的
所以在產生出來的文件部份會有兩部份是一樣的...一部份是要被翻成中
文所以因應員工的要求把它先產生英文..置換內容時比較容易.........

功能:1.讀取複選檔案及資料夾底下之檔案(以FileFilter限定讀取.htm檔)
2.讀出之檔案清單會列在左方清單列表上,底下標籤有顯示路徑用的功能
3.可針對此一清單做儲存清單的動作,工具以JTable來顯示儲存狀態,
並且可做選取清單的動作....(可儲存任何清單...)....
4.Process工具的處理主要由JDOM配合開發一個Manager來管理合併及轉換
文件,Manager透過XML定義的文件資訊讀取相關內容,配合XSLT來輸出新
的HTML檔..日後只需針對XML定義之資訊做修改即可取得不同之內容來
做輸出....
5.針對目前所選之檔案做process,其結果會顯示放Text Area中,中文會亂
碼..這個無大礙所以沒改..同時會輸出新檔放同一層之新資料夾"Result"
中....
6.最後....還有可愛的電腦戰機--Copyright圖案...XD....拿別人畫的來
用..自己也速可以畫啦.@@.不過寫程式比較要緊...XDDDDDDDDDDDDDDDD

暫不改BUG:儲存清單不能改其List Name欄位......會造成錯誤..XD..沒事無聊再改.

工作日:二個星期
完工日期:2003/8/1

評價:架構普通亂...怎麼說呢...我已經盡可能減少物件彼此的複雜度..例如:多
個相關物件以Manager來整合..非相關的物件要使用某物件只需經過Manager
來取得該物件或直接呼叫Method來處理某些動作即可.....聽起來好像很不
錯,但實際上和Design Pattern所使用的方法還是有很多差異性...這種東西
還是要累積經驗才寫的出來...再加油才行...

==============================================使用到下面這個API,沒有的裝一下Big Smile

http://jdom.org/
針對XML做了一套相當直覺性的solution...我用了覺得很好用...推一下..呵.XD
安裝方法...
1. 執行build.bat
2. 針對lib底下的jaxen-jdom.jar和xerces.jar
build底下的jdom.jar
設上path~~~~~
==============================================
最後......就是執行JDoc.bat就可以跑囉....^^....
用裡面的html資料夾檔案做測試.....即可...^^(五個名字不一樣..但內容都一樣...)
XD...我自己亂複製亂改檔名的啦..所以內容一樣咩...

如果開別的非內部特定文件會有錯喔....因為XML沒定義相關資訊不能取資料....
@@....請自己把XML的檔用default裡的覆蓋....目前更新版本有兩種format....
對應到html和html2兩個資料夾.....(第二種format結果怪怪的其實是因為公司要偶這樣做.XD....第一種format也有點怪...因為上下都是同樣文章....都是公司需求啦...用xml的好處是可以隨時換定義....簡單地改變輸出檔....)


flashyang edited on 2003-08-05 10:41
reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
garbage

以死美工自居



發文: 65
積分: 0
於 2003-09-25 04:30 user profilesend a private message to usersend email to garbagereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不是我不給你,我這邊只是有書跟一些案例吧!
在說,我知道你有再用這東西已經是多久以後的事了說...

><"


reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
flashyang

我是巨嘴鳥咕咕~~^Q^



發文: 16
積分: 1
於 2003-10-01 10:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵.....It's ok 啦....
現在為了準備一份報告...狂看和XML有關的技術..@@....真是博大精深咧....
大伙多PO一點和XML有關的技術吧..QQ..人家看規格書和Source看的好累啊~~


reply to postreply to post
作者 Re:想問一下JDOM針對XML&XSLT的處理 [Re:flashyang]
daniel1120

cynthia



發文: 31
積分: 0
於 2003-10-02 08:21 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這是我之前做的(從書上東撿西貼湊成)Big Smile

Questionnaire2html.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.Properties;
import java.io.*;
import java.net.URL;
 
import javax.servlet.*;
import javax.servlet.http.*;
 
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
 
public class Questionnaire2html extends HttpServlet{
  
  private static final String hostname="127.0.0.1";
  private static final int portNumber=80;
  private static final String filePath="/multiteach/";
  
  private static final String stylesheet="/multiteach/XSL/questionnaire2html.xsl";//xsl檔案的位置
  
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
  
    res.setContentType("text/html");
    
    String file=req.getParameter("file");
    
    
    if (file!=null){    
      file=filePath+file;
    }
    else
      return;
    
    URL getXML=new URL("http", hostname, portNumber, file);
    URL getXSLT=new URL("http", hostname, portNumber, stylesheet);
    InputStream inXML=getXML.openStream();
    InputStream inXSLT=getXSLT.openStream();
    
    
    try{
      TransformerFactory factory=TransformerFactory.newInstance();
      factory.setErrorListener(new TransError());
      
      Transformer transformer=factory.newTransformer(new StreamSource(inXSLT));
      transformer.setErrorListener(new TransError());
      
      Properties properties=new Properties();
      properties.put(OutputKeys.METHOD, "html");
      properties.put(OutputKeys.INDENT, "yes");
      properties.put("{http://xml.apache.org/xslt}indent-amount", "2");
      
      transformer.setOutputProperties(properties);
      
      transformer.transform(new StreamSource(inXML), new StreamResult(res.getOutputStream()));
    }
    catch(Exception ex){
      PrintWriter out=res.getWriter();
      out.println("Error: "+ ex.getMessage());
      out.close();
    }
  
  }
 
}


TransError.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import javax.xml.transform.ErrorListener;
import javax.xml.transform.SourceLocator;
import javax.xml.transform.TransformerException;
 
public class TransError implements ErrorListener{
             
  public void warning(TransformerException exception) throws TransformerException{
    SourceLocator locator = exception.getLocator();
    System.err.println("### 警告訊息 ###\n" +
        " 行數: " +
        locator.getLineNumber() + "\n" +
        " URI: " + 
        locator.getSystemId() + "\n" +
        " 訊息: " + 
        exception.getMessage());
  }
 
  public void error(TransformerException exception) throws TransformerException {
    SourceLocator locator = exception.getLocator();
    System.err.println("### 一般性錯誤 ###\n" +
        " 行數: " +
        locator.getLineNumber() + "\n" +
        " URI: " + 
        locator.getSystemId() + "\n" +
        " 訊息: " + 
        exception.getMessage());
  }
 
  public void fatalError(TransformerException exception) throws TransformerException {
    SourceLocator locator = exception.getLocator();
    System.err.println("### 毀滅性錯誤 ###\n" +
        " 行數: " +
        locator.getLineNumber() + "\n" +
        " URI: " + 
        locator.getSystemId() + "\n" +
        " 訊息: " + 
        exception.getMessage());
  }
 
}


只要輸入網址 http://yourhostname/servlet/Questionnaire2html?file=OOOO.xml 就可以看到轉出的html
OOOO→xml的檔案名稱


daniel1120 edited on 2003-10-02 08:23
reply to postreply to post
» JWorld@TW »  Java & XML、Web Service

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8