練功房推薦書單

  • 猛虎出柙雙劍合璧版--最新 OCA / OCP Java SE 7 Programmer 專業認證 (電子書)
  • 流浪教師存零股存到3000萬(全新增修版)(書+DVD)
  • 開始在關西自助旅行(京都‧大阪‧神戶‧奈良)(全新增訂版)
  • 不敗教主的300張股票存股術
JAVA讀取XML一問 for JDOM RSS feed
Forum Index » 網頁程式設計 Web Development
Author Message
crc2121

九級學員

Joined: 2011/1/17
Messages: 10
Offline
現在我只要讀取xml檔案中的標籤(YYY、SSS、NO三個字串),xml檔案格式如下:

<?xml version = '1.0' encoding = 'Big5'?>
<!DOCTYPE ROWSET [
<!ELEMENT ROWSET (ROW)*>
<!ELEMENT ROW (YYY, SSS, NO)> 
<!ATTLIST ROW          num CDATA #REQUIRED>
<!ATTLIST 電子檔案名稱 num CDATA #REQUIRED>
<!ATTLIST 附件名稱     num CDATA #REQUIRED>
<!ATTLIST 附件媒體型式 num CDATA #REQUIRED>
<!ATTLIST 附件數量     num CDATA #REQUIRED>
<!ATTLIST 附件單位     num CDATA #REQUIRED>
<!ATTLIST 主題         num CDATA #REQUIRED>
<!ATTLIST 附註         num CDATA #REQUIRED>
<!ELEMENT YYY (#PCDATA)>
<!ELEMENT SSS (#PCDATA)>
<!ELEMENT NO (#PCDATA)>
]>

<ROWSET>

<ROW num="1">
  <YYY>0080</YYY>
  <SSS>010302</SSS>
  <NO>002</NO>
</ROW>

<ROW num="2">
  <YYY>0078</YYY>
  <SSS>011602</SSS>
  <NO>001</NO>
</ROW>

<ROW num="3">
  <YYY>0070</YYY>
  <SSS>019302</SSS>
  <NO>011</NO>
</ROW>
     ‧
       ‧
     ‧
       ‧
<ROW num="1225">
  <YYY>0099</YYY>
  <SSS>018719</SSS>
  <NO>111</NO>
</ROW>

</ROWSET>


然後我顯示出來我要的資訊及如下程式碼:

-- Output --
年度號: 0080
分類號: 010302
案次號: 002
---------------------------------------
年度號:0078
分類號:011602
案次號:001
---------------------------------------
     ‧
       ‧
       ‧
       ‧
年度號: 0099
分類號: 018719
案次號: 111


import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
 
public class ReadXMLFile {
	public static void main(String[] args) {
 
	  SAXBuilder builder = new SAXBuilder();
	  File xmlFile = new File("C:\\JAVA_WORK\\test3.xml");  //已修正

 
	  try {
 
		Document document = (Document) builder.build(xmlFile);
		Element rootNode = document.getRootElement();
		List list = rootNode.getChildren("ROW");  //已修正
 
		for (int i = 0; i < list.size(); i++) {
 
		   Element node = (Element) list.get(i);
 
		   System.out.println("年度號 : " + node.getChildText("YYY"));
		   System.out.println("分類號 : " + node.getChildText("SSS"));
		   System.out.println("案次號 : " + node.getChildText("NO"));
		   System.out.println("----------------------------------------"); 
		}
 
	  } catch (IOException io) {
		System.out.println(io.getMessage());
	  } catch (JDOMException jdomex) {
		System.out.println(jdomex.getMessage());
	  }
	}
}


仍是有錯?有在想是不是沒安裝JDOM與設定路徑?

目前將http://jdom.org/dist/binary/

的jdom-1.1.2.zip擋下載了。

可以請教一下怎麼安裝與設定路徑嗎?

感激不盡<(_ _)>
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
您的程式需要修改一下,第13行改為兩個倒斜線(\\),為什麼要兩個倒斜線是因為到斜線本身是Java的跳脫字元,否則就會變成\t,這樣就變成跳格字元了。
File xmlFile = new File("C:\\JAVA_WORK\\test3.xml");

第19行改為
List list = rootNode.getChildren("ROW");

即可正常輸出您要的結果。

至於jdom-1.1.2.jar檔的安裝或設定其實主要是要設定在CLASSPATH這個環境變數上,例如您可以將jdom-1.1.2.zip解壓縮,然後將jar檔複製到於C:\JAVA_WORK\lib目錄下,然後在CLASSPATH中加入C:\JAVA_WORK\lib\jdom-1.1.2.jar。

分享經驗 累積智慧
[WWW] [MSN]
crc2121

九級學員

Joined: 2011/1/17
Messages: 10
Offline
原本錯了25個,現在錯了12個(應該是有修正),現在是哪邊有問題?


C:\JAVA_WORK>javac ReadXMLFile.java
ReadXMLFile.java:4: package org.jdom does not exist
import org.jdom.Document;
               ^
ReadXMLFile.java:5: package org.jdom does not exist
import org.jdom.Element;
               ^
ReadXMLFile.java:6: package org.jdom does not exist
import org.jdom.JDOMException;
               ^
ReadXMLFile.java:7: package org.jdom.input does not exist
import org.jdom.input.SAXBuilder;
                     ^
ReadXMLFile.java:12: cannot find symbol
symbol  : class SAXBuilder
location: class ReadXMLFile
          SAXBuilder builder = new SAXBuilder();
          ^
ReadXMLFile.java:12: cannot find symbol
symbol  : class SAXBuilder
location: class ReadXMLFile
          SAXBuilder builder = new SAXBuilder();
                                   ^
ReadXMLFile.java:17: cannot find symbol
symbol  : class Document
location: class ReadXMLFile
                Document document = (Document) builder.build(xmlFile);
                ^
ReadXMLFile.java:17: cannot find symbol
symbol  : class Document
location: class ReadXMLFile
                Document document = (Document) builder.build(xmlFile);
                                     ^
ReadXMLFile.java:18: cannot find symbol
symbol  : class Element
location: class ReadXMLFile
                Element rootNode = document.getRootElement();
                ^
ReadXMLFile.java:23: cannot find symbol
symbol  : class Element
location: class ReadXMLFile
                   Element node = (Element) list.get(i);
                   ^
ReadXMLFile.java:23: cannot find symbol
symbol  : class Element
location: class ReadXMLFile
                   Element node = (Element) list.get(i);
                                   ^
ReadXMLFile.java:34: cannot find symbol
symbol  : class JDOMException
location: class ReadXMLFile
          } catch (JDOMException jdomex) {
                   ^
12 errors

C:\JAVA_WORK>


懇請指點一下,謝謝<(_ _)>
andowson

七段學員
[Avatar]

Joined: 2007/1/2
Messages: 706
Location: 台北
Offline
您可以參考一下這個連結的說明:
http://caterpillar.onlyfun.net/Gossip/JavaEssence/WhatClasspath.html

先弄清楚什麼是CLASSPATH後,這個問題您應該就可以自行處理了。

分享經驗 累積智慧
[WWW] [MSN]
 
Forum Index » 網頁程式設計 Web Development
Go to:   
Mobile view