JAVA获取XML文件并输出
比如,现在有一个名为1.xml的文件,放在h:\1.xml;它的内容是:-<class>-<student><name>A</name><age>20</age><sco...
比如,现在有一个名为1.xml的文件,放在h:\1.xml; 它的内容是:
- <class>
- <student>
<name>A</name>
<age>20</age>
<score>80</score>
</student>
- <student>
<name>B</name>
<age>30</age>
<score>90</score>
</student>
- <student>
<name>C</name>
<age>40</age>
<score>100</score>
</student>
</class>
如何写java的代码,让java读取这个xml文件后按年龄升序将数据输出到控制台? 展开
- <class>
- <student>
<name>A</name>
<age>20</age>
<score>80</score>
</student>
- <student>
<name>B</name>
<age>30</age>
<score>90</score>
</student>
- <student>
<name>C</name>
<age>40</age>
<score>100</score>
</student>
</class>
如何写java的代码,让java读取这个xml文件后按年龄升序将数据输出到控制台? 展开
2个回答
展开全部
// 给你一个例子,你参考下
// java通过dom读写xml文件
/*要读的xml文件
<?xml version="1.0" encoding="GB2312"?>
<学生花名册>
<学生 性别 = "男">
<姓名>李华</姓名>
<年龄>14</年龄>
</学生>
<学生 性别 = "男">
<姓名>张三</姓名>
<年龄>16</年龄>
</学生>
</学生花名册>
*/
package xml;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class DomTest {
Vector students_Vector;
private Vector readXMLFile(String file) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(file); // 获取到xml文件
// 下面开始读取
Element root = doc.getDocumentElement(); // 获取根元素
NodeList students = root.getElementsByTagName_r("学生");
students_Vector = new Vector();
for (int i = 0; i < students.getLength(); i++) {
// 一次取得每一个学生元素
Element ss = (Element) students.item(i);
// 创建一个学生的实例
student stu = new student();
stu.setSex(ss.getAttribute("性别"));
NodeList names = ss.getElementsByTagName_r("姓名");
Element e = (Element) names.item(0);
Node t = e.getFirstChild();
stu.setName(t.getNodeValue());
NodeList ages = ss.getElementsByTagName_r("年龄");
e = (Element) ages.item(0);
t = e.getFirstChild();
stu.setAge(Integer.parseInt(t.getNodeValue()));
students_Vector.add(stu);
}
return students_Vector;
}
// 写入xml文件
public static void callWriteXmlFile(Document doc, Writer w, String encoding) {
try {
Source source = new DOMSource(doc);
Result result = new StreamResult(w);
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
private void writeXMLFile(String outfile) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = dbf.newDocumentBuilder();
} catch (Exception e) {
}
Document doc = builder.newDocument();
Element root = doc.createElement("学生花名册");
doc.appendChild(root); // 将根元素添加到文档上
// 获取学生信息
for (int i = 0; i < students_Vector.size(); i++) {
student s = (student) students_Vector.get(i);
// 创建一个学生
Element stu = doc.createElement("学生");
stu.setAttribute("性别", s.getSex());
root.appendChild(stu);// 添加属性
// 创建文本姓名节点
Element name = doc.createElement("姓名");
stu.appendChild(name);
Text tname = doc.createTextNode(s.getName());
name.appendChild(tname);
//创建文本年龄节点
Element age = doc.createElement("年龄");
stu.appendChild(age); // 将age添加到学生节点上
Text tage = doc.createTextNode(String.valueOf(s.getAge()));
age.appendChild(tage); // 将文本节点放在age节点上
}
try {
FileOutputStream fos = new FileOutputStream(outfile);
OutputStreamWriter outwriter = new OutputStreamWriter(fos);
// ((XmlDocument)doc).write(outwriter); //出错!
callWriteXmlFile(doc, outwriter, "gb2312");
outwriter.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
String str = "xml/student.xml";
DomTest t = new DomTest();
try {
Vector v = t.readXMLFile(str);
Iterator it = v.iterator();
while (it.hasNext()) {
student s = (student) it.next();
System.out.println(s.getName() + "\t" + s.getAge() + "\t"
+ s.getSex());
}
} catch (Exception e) {
e.printStackTrace();
}
String outfile = "xml/stucopy.xml";
t.writeXMLFile(outfile);
}
}
class student{
private String sex;
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(String s){sex=s;}
public String getSex(){return sex;}
public void setName(String n){name=n;}
public String getName(){return name;}
}
展开全部
import net.sf.saxon.*;
import net.sf.saxon.s9api.*;
public class S9XQueryExample {
public static void main(String[] args) throws Exception {
Processor processor = new Processor(new Configuration());
XQueryCompiler compiler = processor.newXQueryCompiler();
XQueryEvaluator evaluator = compiler.compile("for $s in doc('classes.xml')//student order by number($s/score) return string-join(($s/name,$s/age,$s/score), ' ')").load();
XdmValue value = evaluator.evaluate();
for(XdmItem item : value){
System.out.println(item.getStringValue());
}
evaluator.close();
}
}
javac -cp saxon9he.jar S9XQueryExample.java
java -cp saxon9he.jar;. S9XQueryExample
A 20 80
B 30 90
C 40 100
更多追问追答
追问
这个jar文件应该怎么用?
追答
就是一个类库,eclipse可以加到build path,命令行就像上面那样运行。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |