如何获取xml文件中某个节点的值?

Destiny_0311 2012-09-28 06:07:54
RT
...全文
599 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
aries90 2012-09-29
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?>

<用户信息>
<用户 用户密码="1" 用户身份="超级管理员" id="2">admin</用户>
<用户 用户密码="1" 用户身份="管理员" id="2">admin1</用户>
<用户 用户密码="2" 用户身份="供应商" id="3">yh</用户>
</用户信息>
aries90 2012-09-29
  • 打赏
  • 举报
回复
package com.anxin.dod;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.anxin.beans.*;

public class Login {
public static String saveXmlFilePath;
T_yonghu c=new T_yonghu();
//SAXReader reader=new SAXReader();//构建sax读取对象
List<T_yonghu> list = new ArrayList<T_yonghu>();


public T_yonghu readXML(String yhmc,String yhma,String path) throws IOException{

Document doc=(Document) Login.getXmlDocument(path);
Element ele=doc.getRootElement();

List eles=ele.elements("用户");

//System.out.println("根节点:"+ele.getName());
//Iterator it=root.elementIterator();//获取根节点下面的所有子节点

for(Iterator childs=eles.iterator();childs.hasNext();){

Element el = (Element)childs.next();
//System.out.println("yonghu:"+el.getText());
// System.out.println("yonghu:"+el.attributeValue("用户密码"));
if(yhmc.equals(el.getText()) && yhma.equals(el.attributeValue("用户密码")))

{ c.setYhmc(el.getText());
//c.setYhma(el.element("用户名称").attributeValue("用户密码"));
c.setYhsf(el.attributeValue("用户身份"));
c.setId(el.attributeValue("id"));
//list.add(c);
}

}
return c;
}


public static Document getXmlDocument(String saveXmlFilePath){

setSaveXmlFilePath(saveXmlFilePath);

SAXReader reader = new SAXReader();

Document doc = null;

try {

doc = reader.read(new File(saveXmlFilePath));

}finally{

return doc;

}

}



public static void saveDocument(Document doc) throws IOException{

OutputFormat format = OutputFormat.createPrettyPrint();

XMLWriter writer = new XMLWriter(new FileOutputStream(saveXmlFilePath),format);

writer.write(doc);

writer.close();

}


public static String getSaveXmlFilePath() {

return saveXmlFilePath;

}



public static void setSaveXmlFilePath(String saveXmlFilePath) {

Login.saveXmlFilePath = saveXmlFilePath;

}
}


Luger 2012-09-29
  • 打赏
  • 举报
回复
在网上查查 java 解析 xml 很多例子的
xulin5215511 2012-09-29
  • 打赏
  • 举报
回复
dom4j 是很很强大,还有更强大的,xpath,它和正则类似,可以获取任意节点,有兴趣看看
butnet 2012-09-28
  • 打赏
  • 举报
回复

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class XmlTest {
/**
* @author butnet
* @param args
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException {
Document doc = DocumentHelper.parseText("<user><name>butnet</name><web>http://jvmer.com</web></user>");
Element user = doc.getRootElement();
Element name = user.element("name");
Element web = user.element("web");

System.out.println(name.getText());
System.out.println(web.getText());
}
}

/*使用DOM4J可以很方便的对XML进行读写, 项目地址 http://dom4j.sourceforge.net/ */

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧