把它改成下面的样子:<?xml version="1.0" encoding="UTF-8" ?>
<Test xmlns:LIT="http://ABCD/">
<LIT:student>
  <LIT:name>bigmouse</LIT:name>
  <LIT:sex>male</LIT:sex>
  <LIT:lesson>
     <LIT:lessonName>math</LIT:lessonName>
     <LIT:lessonScore>60</LIT:lessonScore>
  </LIT:lesson>
  <LIT:lesson>
     <LIT:lessonName>Englist</LIT:lessonName>
     <LIT:lessonScore>59</LIT:lessonScore>
  </LIT:lesson>
</LIT:student>
  <LIT:breakLine />
     <LIT:student>
        <LIT:name>coco</LIT:name>
        <LIT:sex>female</LIT:sex>
     <LIT:lesson>
         <LIT:lessonName>math</LIT:lessonName>
         <LIT:lessonScore>90</LIT:lessonScore>
     </LIT:lesson>
     <LIT:lesson>
          <LIT:lessonName>Englist</LIT:lessonName>
          <LIT:lessonScore>95</LIT:lessonScore>
     </LIT:lesson>
</LIT:student>
</Test>然后利用JDom,或者类似的API就可以去取值了。

解决方案 »

  1.   

    import org.jdom.*;
    import org.jdom.input.*;
    import java.io.*;
    import java.util.*;public class TestMain {
      public static void main(String[] args) {
        try
        {
          SAXBuilder parser = new SAXBuilder();
          Document doc = parser.build(new File("C:\\Test.xml"));
          Element root = doc.getRootElement();
          Namespace ns = root.getNamespace();
          Element student = root.getChild("student", ns);
          List lessons = student.getChildren("lesson",ns);
          Iterator oI = lessons.iterator();
          while(oI.hasNext())
          {
            Element lesson = (Element)oI.next();
            String lessonName = lesson.getChild("lessonName",ns).getText();
            String lessonScore = lesson.getChild("lessonScore",ns).getText();
            System.out.println("LessonName: " + lessonName);
            System.out.println("LessonScore: " + lessonScore + "\n");
          }
        }
        catch(JDOMException e)
        {
          System.out.println(e.toString());
        }
      }
    }这里假设Test.xml就包含你的下面的那段XML.记住元素Test的名字我改了一下!
     
    <?xml version="1.0" encoding="UTF-8" ?>
    <LIT:Test xmlns:LIT="http://ABCD/">
    <LIT:student>
      <LIT:name>bigmouse</LIT:name>
      <LIT:sex>male</LIT:sex>
      <LIT:lesson>
         <LIT:lessonName>math</LIT:lessonName>
         <LIT:lessonScore>60</LIT:lessonScore>
      </LIT:lesson>
      <LIT:lesson>
         <LIT:lessonName>Englist</LIT:lessonName>
         <LIT:lessonScore>59</LIT:lessonScore>
      </LIT:lesson>
    </LIT:student>
    </LIT:Test>
      

  2.   

    xml文件如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE LIT:StuInfo SYSTEM "dtd\student.dtd">
    <?xml-stylesheet href="xsl\StuInfo.xsl" type="text/xsl"?>
    <LIT:StuInfo xmlns:LIT="http://www.lit.edu.cn/student/">
    <LIT:student>
      <LIT:name>bigmouse</LIT:name>
      <LIT:sex>male</LIT:sex>
      <LIT:lesson>
         <LIT:lessonName>math</LIT:lessonName>
         <LIT:lessonScore>60</LIT:lessonScore>
      </LIT:lesson>
      <LIT:lesson>
         <LIT:lessonName>Englist</LIT:lessonName>
         <LIT:lessonScore>59</LIT:lessonScore>
      </LIT:lesson>
    </LIT:student>
      <LIT:breakLine />
         <LIT:student>
            <LIT:name>coco</LIT:name>
            <LIT:sex>female</LIT:sex>
         <LIT:lesson>
             <LIT:lessonName>math</LIT:lessonName>
             <LIT:lessonScore>90</LIT:lessonScore>
         </LIT:lesson>
         <LIT:lesson>
              <LIT:lessonName>Englist</LIT:lessonName>
              <LIT:lessonScore>95</LIT:lessonScore>
         </LIT:lesson>
    </LIT:student>
       <LIT:breakLine />
         <LIT:student>
           <LIT:name>fannWong</LIT:name>
           <LIT:sex>female</LIT:sex>
          <LIT:lesson>
             <LIT:lessonName>math</LIT:lessonName>
             <LIT:lessonScore>85</LIT:lessonScore>
          </LIT:lesson>
          <LIT:lesson>
              <LIT:lessonName>English</LIT:lessonName>
                <LIT:lessonScore>95</LIT:lessonScore>
           </LIT:lesson>
       </LIT:student>
    <LIT:breakLine />
    <LIT:master>
    &masterName; 
    </LIT:master>
    </LIT:StuInfo>
    其实我要做的是一个函数:getString(String strdir,String filename,Vector vv,Vector id)
    strdir:xml文件所在目录
    filename:xml的文件名
    vv:要查的元素名
    id:要查的元素的属性id,可为空
    根本不知道如上student元素的子元素有什么,结构是什么?我需要取出每个元素名和VV中的值和属性比较,如果一致才取值。
    明白我得意思吗?高手请帮忙?