public List<Student> doReadXML(String path) throws Exception {
List<Student> empvoList = new ArrayList<Student>();
File file = new File(path);
//输入流对象
FileInputStream fis = new FileInputStream(file);
//jdom解析器
SAXBuilder sb = new SAXBuilder();
Document doc= sbbuild(fis);
//获得XML的根元素
Element root = docgetRootElement();
//获得根元素下的所有子元素
List<Element> employees = rootgetChildren();
for(int i=0;i<employeessize();i++){
Element employee =employeesget(i);
Student stu= new Student();
String name = employeegetChildText("name");
String sex = employeegetChildText("sex");
String agetemp = employeegetChildText("age");
String home = employeegetChildText("home");
String email = employeegetChildText("email");
stusetName(name);
stusetSex(sex);
int age = 0;
if(agetempequals("")){
age = 0;
} else {
age = IntegerparseInt(agetemp);
}
stusetAge(age);
stusetHome(home);
stusetEmail(email);
Systemoutprintln(name+"\t"+i);
empvoListadd(stu);
}
return empvoList;
}
仅供看考。xml也是文本文件,按照文本文件的方法读取就可以了。
以下仅供参考
package comkiddtestzhidao;import javaioBufferedReader;
import javaioFile;
import javaioFileInputStream;
import javaioIOException;
import javaioInputStreamReader;
/
Hello world!
/
public class Execute {
public static void main(String[] args) throws InterruptedException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\123xml"))));
String string;
while ((string = brreadLine()) != null) {
Systemoutprintln(string);
}
brclose();
}
}
在java环境下读取xml文件的方法主要有4种:DOM、SAX、JDOM、JAXB
1 DOM(Document Object Model)
此方法主要由W3C提供,它将xml文件全部读入内存中,然后将各个元素组成一棵数据树,以便快速的访问各个节点 。 因此非常消耗系统性能 ,对比较大的文档不适宜采用DOM方法来解析。 DOM API 直接沿袭了 XML 规范。每个结点都可以扩展的基于 Node 的接口,就多态性的观点来讲,它是优秀的,但是在 Java 语言中的应用不方便,并且可读性不强。
实例:
import javaxxmlparsers;
//XML解析器接口
import orgw3cdom;
//XML的DOM实现
import orgapachecrimsontreeXmlDocument;
//写XML文件要用到
DocumentBuilderFactory factory = DocumentBuilderFactorynewInstance();
//允许名字空间
factorysetNamespaceAware(true);
//允许验证
factorysetValidating(true);
//获得DocumentBuilder的一个实例
try {
DocumentBuilder builder = factorynewDocumentBuilder();
} catch (ParserConfigurationException pce) {
Systemerrprintln(pce);
// 出异常时输出异常信息,然后退出,下同
Systemexit(1);
}
//解析文档,并获得一个Document实例。
try {
Document doc = builderparse(fileURI);
} catch (DOMException dom) {
Systemerrprintln(domgetMessage());
Systemexit(1);
} catch (IOException ioe) {
Systemerrprintln(ioe);
Systemexit(1);
}
//获得根节点StuInfo
Element elmtStuInfo = docgetDocumentElement();
//得到所有student节点
NodeList nlStudent = elmtStuInfogetElementsByTagNameNS(
strNamespace, "student");
for (……){
//当前student节点元素
Element elmtStudent = (Element)nlStudentitem(i);
NodeList nlCurrent = elmtStudentgetElementsByTagNameNS(
strNamespace, "name");
}
import javaioFile;
import javaxxmlparsersDocumentBuilder;
import javaxxmlparsersDocumentBuilderFactory;
import orgw3cdomDocument;
import orgw3cdomElement;
import orgw3cdomNodeList;
public class Xml {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
newInstance();
DocumentBuilder builder = factorynewDocumentBuilder();
Document document = builderparse(new File("E:\\新建 文本文档 (3)xml"));
Element rootElement = documentgetDocumentElement();
NodeList list = rootElementgetElementsByTagName("Header");
Element element = (Element) listitem(0);
Systemoutprintln(elementgetChildNodes()item(0)getNodeValue());
} catch (Exception e) {
Systemoutprintln("exception:" + egetMessage());
}
}
}
直接附源码import javaioFileWriter;
import javaioIOException;
import javautilIterator;import orgdom4j;
import orgdom4jioXMLWriter;
public class Dom4jSample { public static void main(String[] args) {
Dom4jSample dom4jSample = new Dom4jSample();
Document document = dom4jSamplecreateDocument();
try{
dom4jSampleFileWrite(document);
Document documentStr = dom4jSampleStringToXML("<China>I Love!</China>");
dom4jSampleXMLWrite(documentStr);
Element legend = dom4jSampleFindElement(document);
Systemoutprintln(legendgetText());
}
catch(Exception e)
{
}
}
/
Create a XML Document
/
public Document createDocument()
{
Document document = DocumentHelpercreateDocument();
Element root = documentaddElement("root");
Element author1 = rootaddElement("Lynch");
author1addAttribute("Age","25");
author1addAttribute("Country","China");
author1addText("I am great!");
Element author2 = rootaddElement("Legend");
author2addAttribute("Age","25");
author2addAttribute("Country","China");
author2addText("I am great!too!");
return document;
}
/
Create a XML document through String
/
public Document StringToXML(String str) throws DocumentException
{
Document document = DocumentHelperparseText(str);
return document;
}
public Element FindElement(Document document)
{
Element root = documentgetRootElement();
Element legend = null;
for(Iterator i=rootelementIterator("legend");ihasNext();)
{
legend = (Element)inext();
}
return legend;
}
/
Write a XML file
/
public void FileWrite(Document document) throws IOException
{
FileWriter out = new FileWriter("C:/Dom2jSamplexml");
documentwrite(out);
outclose();
}
/
Write a XML format file
/
public void XMLWrite(Document document) throws IOException
{
XMLWriter writer = new XMLWriter(new FileWriter("C:/Dom2jSampleStrxml"));
writerwrite(document);
writerclose();
}
}
相对路径的话,可以先获取到当前文件的编译路径,之后在找到想找文件的路径的思路来实现。
举例:
XMLSclassgetClass()getResourceAsStream("/test/testxml");
解释:XMLSclassgetClass()是获取当前的类编译路径,之后通过getResourceAsStream的形式即可找到要读取的文件的路径。
备注:这个方法中后面的路径也可以通过截取的形式来进行路径获取,实现原理都是找到当前类路径,之后通过相对位置找到另外文件路径。
以上就是关于java如何读取xml文件全部的内容,包括:java如何读取xml文件、求高手帮忙JAVA 读取XML 文件,下面是我的XML文件,怎么在JAVA中 把他们全部显示出来。、在java在如何解析XML文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)