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文件 [精華]
ray_linn

什么都不懂的小白

版主

發文: 540
積分: 10
於 2003-07-09 18:33 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的好處就是簡單.

這是我們要建的xml:

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

準備好樂么?let us go!

第一步,創建根元素

Element carElement = new Element("car");
Document myDocument = new Document(carElement);

因爲一個 XML 文檔必須一直有一個唯一的根元素,所以 Document 將 Element 放在它的構造器中。

第二步,添加一個 Attribute

carElement.addAttribute(new Attribute("vin", "123fhg5869705iop90"));

第三步,添加其他元素

carElement.addContent(new Element("make").addContent("Toyota"));

carElement.addContent(new Element("model").addContent("Celica"));
carElement.addContent(new Element("year").addContent("1997"));
carElement.addContent(new Element("color").addContent("green"));
carElement.addContent(new Element("license").addContent("1ABC234").addAttribute("state", "CA"));

第四步,添加一條註釋

carElement.addContent(new Comment("Description of a car"));

第五步,讀一個子元素

Element yearElement = carElement.getChild("year");

第六步,刪除子元素

boolean removed = carElement.removeChild("year");

第七步,存盤

FileWriter writer = new FileWriter("/some/directory/myFile.xml");
outputter.output(myDocument, writer);
writer.close();


ray_linn edited on 2003-07-09 20:46
reply to postreply to post
飞翔的候鸟
作者 Re:用jdom來創建xml文件 [Re:ray_linn]
cfsimon





發文: 16
積分: 0
於 2004-11-25 19: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
請問如果我有個 DTD, 如何去verify 這個 XML呢?

如果是讀入XML檔.
應該是這樣吧
1
2
3
4
SAXBuilder builder = new SAXBuilder();
builder.setValidation(true);
Document doc = null;
doc = builder.build(XMLSTRING, dtdFile);

這樣一路修改並catch exception便可以了.

但如果新建一個又如何做呢?


reply to postreply to post
作者 Re:用jdom來創建xml文件 [Re:ray_linn]
jackthing





發文: 31
積分: 0
於 2005-02-28 10:53 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
你好,想請問若我知道要動態產生<Expression operator="and">的次數
那 要用什麼指令才能 階層式的產生 <Expression operator="and">

寫法是如何寫呢
謝謝大家

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<Condition>
      <Expression operator="and">
        <Expression operator="and">
          <Expression operator="and">
            <Expression operator="=">
              <Expression operator="Fact"  value="a" />
              <Expression operator="Const"  value="false" type="Boolean" />
            </Expression>
            <Expression operator="=">
              <Expression operator="Fact"  value="a" />
              <Expression operator="Const"  value="false" type="Boolean" />
            </Expression>
          </Expression>
          <Expression operator="=">
            <Expression operator="Fact"  value="a" />
            <Expression operator="Const"  value="false" type="Boolean" />
          </Expression>
        </Expression>
        <Expression operator="=">
          <Expression operator="Fact"  value="a" />
          <Expression operator="Const"  value="false" type="Boolean" />
        </Expression>
      </Expression>
    </Condition>


reply to postreply to post
作者 Re:用jdom來創建xml文件 [Re:ray_linn]
jackthing





發文: 31
積分: 0
於 2005-03-03 18:35 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
程式寫出來嚕,如下
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
<%@ page import="java.io.*" %>
<%@ page import="org.jdom.*" %>
<%@ page import="org.jdom.output.XMLOutputter" %>
<%@ page contentType="text/html; charset=Big5" 
   import="java.sql.*"%>
<%
    // 建立根元素 <Condition>         
     Element rootElement = new Element("Rule");      
     Document document = new Document(rootElement); 
     
     Element Condition = new Element("Condition");      
     rootElement.addContent(Condition);
       
       Element andEle = new Element("Expression");
       andEle.setAttribute("operator","and");
  for( int i=0; i<3; i++)
  {
       Element temp = new Element("Expression");
       temp.setAttribute("operator","and");
       temp.addContent(andEle);
       andEle = temp;
  }
  Condition.addContent(andEle);
 
%>
 <%    
     //============================================
     //============================================
         
     // 輸出XML文件      
     XMLOutputter outputter = new XMLOutputter();      
     
     // 設定編碼      
     outputter.setEncoding("Big5");      
     
     
     // 設定原始檔是否換行和縮排      
     outputter.setNewlines(true);      
     outputter.setIndent(true);      
     outputter.output(document, out); 
     
     //XML另存新檔
     FileWriter writer = new FileWriter("/jakarta-tomcat-4.0.3/webapps/medical/test.xml");
     outputter.output(document, writer);
     writer.close(); 
     
%>


結果為
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="Big5"?>
<Rule>
  <Condition>
    <Expression operator="and">
      <Expression operator="and">
        <Expression operator="and">
          <Expression operator="and" />
        </Expression>
      </Expression>
    </Expression>
  </Condition>
</Rule>


reply to postreply to post
作者 Re:用jdom來創建xml文件 [Re:ray_linn]
jazzG





發文: 78
積分: 0
於 2006-05-10 16:58 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
版主,您的程式有誤! 原程式如下:
1
2
3
4
5
6
第二步,添加一個 Attribute 
carElement.addAttribute(new Attribute("vin", "123fhg5869705iop90")); 
 
第三步,添加其他元素 
......
carElement.addContent(new Element("license").addContent("1ABC234").addAttribute("state", "CA")); 


正確應該是:
1
2
3
4
5
6
第二步,添加一個 Attribute 
carElement.setAttribute(new Attribute("vin", "123fhg5869705iop90")); 
 
第三步,添加其他元素 
......
carElement.addContent(new Element("license").addContent("1ABC234").setAttribute("state", "CA")); 


reply to postreply to post
作者 Re:用jdom來創建xml文件 [Re:jackthing]
szl





發文: 16
積分: 0
於 2006-05-17 18:13 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
jackthing wrote:
程式寫出來嚕,如下
1
2
3
4
5
6
7
     // 設定編碼      
     outputter.setEncoding("Big5");      
     
     
     // 設定原始檔是否換行和縮排      
     outputter.setNewlines(true);      
     outputter.setIndent(true);      



jdom目前的API似乎沒有這樣的方法耶
http://www.jdom.org/docs/apidocs/org/jdom/output/XMLOutputter.html
不知道大家是用那一版的jdom
我是用v1.0的
不然我的xml都不會換行還有縮排


reply to postreply to post
作者 Re:用jdom來創建xml文件 [Re:ray_linn]
szl





發文: 16
積分: 0
於 2006-05-18 17:39 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
原來那個format就是這時候用的
1
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());

還有很多format的用法
http://www.jdom.org/docs/apidocs/org/jdom/output/Format.html


reply to postreply to post
作者 Re:用jdom來創建xml檔案 [Re:szl]
xsmall

睜大雙眼~看透這個世界



發文: 78
積分: 0
於 2006-07-21 10:01 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
大家好,我把該程式由jsp改成app形式如下:
import java.io.*;
import org.jdom.*;
import org.jdom.output.Format;
import java.sql.*;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;

// 建立根元素 <Condition>
public class createXml{
    public static void main(String args[]){
try{

//===================================
Element rootElement = new Element("Rule");
Document document = new Document(rootElement);

Element Condition = new Element("Condition");
rootElement.addContent(Condition);

Element andEle = new Element("Expression");
andEle.setAttribute("operator","and");
for( int i=0; i<3; i++)
{
Element temp = new Element("Expression");
temp.setAttribute("operator","and");
temp.addContent(andEle);
andEle = temp;
}
Condition.addContent(andEle);

// 輸出XML檔案
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());

// 設定編碼
outputter.setEncoding("Big5");

// 設定原始檔是否換行和縮排
outputter.setNewlines(true);
outputter.setIndent(true);
outputter.output(document,out);

//XML另存新檔*/

FileWriter writer = new FileWriter("test.xml");
outputter.output(document,writer);
writer.close();
}
catch(Exception e){}
}
}

===>但為何都抓不到setEncoding(),setNewlines(),setIndent()等方法?
原程式是直接由XMLOutputter類別來產生outputter,但這些method其實是在Format的類別中,改成XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());這樣,為何還是不能執行?~請指導一下..thx


reply to postreply to post
不適合喝咖啡的我~卻愛上它那特有的淡淡香氣....Orz
好在還有茶能代替.....品茗咧~
作者 Re:用jdom來創建xml文件 [Re:ray_linn]
afra_hung





發文: 2
積分: 0
於 2007-03-26 17:52 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
我是這樣寫的..

//set XML format
Format fmt = Format.getPrettyFormat();
fmt = fmt.setEncoding("big5");
XMLOutputter xmlOutputter = new XMLOutputter(fmt);
//set XML format -----end


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