把它改成下面的样子:<?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就可以去取值了。
<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就可以去取值了。
解决方案 »
- 想修改displaytag的css样式在哪儿修改啊?他的css文件叫什么名字
- tomcat6.0连接池连接oracle失败
- 这段代码可不可以精简一下?
- 谁会dom4j解析xml
- struts启动tomcat报错了
- 新手求助,有一个功能不会实现,希望大神来看看!请教请教!!!
- -------------------------XML-----------------------------SOS------------------------XML---------------------------
- 请帮忙看看 这个名字为什么是空的呢?
- 新手!!Hibernate查询的问题,求大神们指点一下!!
- 关于IntelliJ IDEA发布WEB项目时输出路径下文件夹冗余的问题
- 怎么在JB7中加入MySQL的JDBC驱动啊?!
- 在tcp/ip编程中如何才能访问对方的硬盘??高手请进。。。。。。
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>
<?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中的值和属性比较,如果一致才取值。
明白我得意思吗?高手请帮忙?