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

» JavaWorld@TW » Java & XML、Web Service  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 请问java使用xerces的环境如何配置?我都没有成功过。
yoshubom





發文: 105
積分: 0
於 2006-08-18 17:02 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
我从网上下载了 Xerces-J-bin.2.5.0.zip ,解压之后里面有四个jar文件。

我要读取的xml文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="gb2312"?>
<roster>
  <student ID="s101">
    <name>李华</name>
    <sex>男</sex>
    <birthday>1978.9.12</birthday>
    <score>92</score>
    <skill>Java</skill>
    <skill>Oracle</skill>
    <skill>C Sharp</skill>
    <skill>SQL Server</skill>
  </student>
  <student ID="s101">
    <name>齐辉</name>
    <sex>女</sex>
    <birthday>1979.3.2</birthday>
    <score>90</score>
    <skill>Java</skill>
    <skill>Oracle</skill>
  </student>
</roster>


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
import java.io.*;
import org.w3c.dom.*;
import org.w3c.dom.Node;
import org.xml.sax.*;
import org.apache.xerces.parsers.*;
 
public class DOMSample {
  public static void main(String []argv){
    try {
      if (argv.length != 1){
        System.err.println("Usage: java DOMSample filename");
        System.exit(1);
      }
      BufferedReader in = new BufferedReader(new FileReader(argv[0]));
      DOMParser parser = new DOMParser();
      parser.parse(new InputSource(in));
      Document doc = parser.getDocument();
      System.out.println("元素是:");
      printElements(doc);
      System.out.println();
      System.out.println("每个元素的属性是:");
      printElementAttributes(doc);
    }catch(SAXException s){
      System.out.println(s.toString());
    }catch(IOException e){
      System.out.println(e.toString());
    }
  }
  
  static void printElements(Document doc){
    NodeList nl = doc.getElementByTagName("*");
    Node n;
    for (int i=0;i<nl.getLength();i++){
      n = nl.item(i);
      System.out.print(n.getNodeName() + " ");
    }
  }
  
  static void printElementAttributes(Document doc){
    NodeList nl = doc.getElementsByTagName("*");
    Element e;
    Attr attr;
    NamedNodeMap nnm;
    String attrname;
    String attrval;
    int i,len;
    len = nl.getLength();
    for (int j=0;j<len;j++){
      e = (Element)nl.item(j);
      System.out.println(e.getTagName() + ":" + e.getFirstChild().getNodeValue());
      nnm = e.getAttributes();
      if (nnm != null){
        for (i=0;i<nnm.getLength();i++){
          attr = (Attr)nnm.item(i);
          attrname = attr.getName();
          attrval = attr.getValue();
          System.out.println("属性是:" + attrname + " = " + attrval);
        }
      }
    }
  }
}


但是编译的时候总是出现这样的错误:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
DOMSample.java:5: 软件包 org.apache.xerces.parsers 不存在
import org.apache.xerces.parsers.*;
^
DOMSample.java:15: 找不到符号
符号: 类 DOMParser
位置: 类 DOMSample
                        DOMParser parser = new DOMParser();
                        ^
DOMSample.java:15: 找不到符号
符号: 类 DOMParser
位置: 类 DOMSample
                        DOMParser parser = new DOMParser();
                                               ^
DOMSample.java:31: 找不到符号
符号: 方法 getElementByTagName(java.lang.String)
位置: 接口 org.w3c.dom.Document
                NodeList nl = doc.getElementByTagName("*");
                                 ^
4 错误


我已经在环境变量里将解压出来的四个jar文件加到CLASSPATH里面了呀。到底要怎么做才对,请高手指定,谢谢!


vote up 0 vote down
reply to postreply to post
作者 Re:请问java使用xerces的环境如何配置?我都没有成功过。 [Re:yoshubom]
yoshubom





發文: 105
積分: 0
於 2006-08-19 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
帮帮我呀,各位大哥。

vote up 0 vote down
reply to postreply to post
作者 Re:请问java使用xerces的环境如何配置?我都没有成功过。 [Re:yoshubom]
kebin_liu

沿著地表飛行

版主

發文: 1812
積分: 11
於 2006-08-19 10:50 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
把DOMSample.java和四個jar放一起,然後下命令
javac -classpath ./xmlParserAPIs.jar;./xercesImpl.jar;./xercesSamples.jar;./xml-apis.jar DOMSample.java

此時你應該會發現你31行寫錯了,少一個s doc.getElementsByTagName("*");

改正就可以編譯了。


vote up 0 vote down
reply to postreply to post
作者 Re:请问java使用xerces的环境如何配置?我都没有成功过。 [Re:kebin_liu]
yoshubom





發文: 105
積分: 0
於 2006-08-19 10: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
我试试看,多谢指点。呵呵。

vote up 0 vote down
reply to postreply to post
作者 Re:请问java使用xerces的环境如何配置?我都没有成功过。 [Re:yoshubom]
yoshubom





發文: 105
積分: 0
於 2006-08-19 11: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
多谢,真的可以哦。将JAVA文件和XML文件copy过去之后就OK了。但是如果我不想这么麻烦,直接写入环境变量中要如何操作呢?

vote up 0 vote down
reply to postreply to post

» JavaWorld@TW »  Java & XML、Web Service

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

JavaWorld@TW


Powered by Powerful JuteForum® Version Jute 1.5.8
Copyright© 2002-2003 Rainman Zhu,Zua,Netboy,Scott. All Rights Reserved.