SAXReader reader = new SAXReader(); String name = ""; try { Document doc = reader.read(xmlPath); //Node root = doc.selectSingleNode("/system"); //List list = root.selectNodes("subsystem[@name='SA']/prefixurl"); Node root = doc.selectSingleNode(rootName); List list = root.selectNodes(nodeName); for(int i = 0; i < list.size(); i++){ Element e = (Element) list.get(i); name += e.getText()+"\n"; }
* 三个参数分别是XML文件路径,根结点,以及要查找的接点
*/
public static String parseXML(String xmlPath, String rootName, String nodeName){
SAXReader reader = new SAXReader();
String name = "";
try {
Document doc = reader.read(xmlPath);
//Node root = doc.selectSingleNode("/system");
//List list = root.selectNodes("subsystem[@name='SA']/prefixurl");
Node root = doc.selectSingleNode(rootName);
List list = root.selectNodes(nodeName);
for(int i = 0; i < list.size(); i++){
Element e = (Element) list.get(i);
name += e.getText()+"\n";
}
} catch (Exception e) {
e.printStackTrace();
}
return name;
}
public class XmlProcessor {
private Document doc=null;
/**
* param1 xmlPath 指定XML文件的本地路径
* @param xmlPath
*/
public XmlProcessor(String xmlPath){
//很明显该类是一个单例,先获取产生DocumentBuilder工厂
//的工厂,在通过这个工厂产生一个DocumentBuilder,
//DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
//这个Document就是一个XML文件在内存中的镜像
doc=db.parse(new File(xmlPath));
}catch(Exception e){
System.out.println("获取文档对象失败");
}
}
/**
*
* @param objInputStream XML 文件的InputStream输入流
*/
public XmlProcessor(InputStream objInputStream){
//很明显该类是一个单例,先获取产生DocumentBuilder工厂
//的工厂,在通过这个工厂产生一个DocumentBuilder,
//DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
//这个Document就是一个XML文件在内存中的镜像
doc=db.parse(objInputStream);
}catch(Exception e){
System.out.println("获取文档对象失败");
}
}
public String getElementContent(String elementName){
String strReturn="";
try{
NodeList nodeList=doc.getElementsByTagName(elementName);
/*
Element el1=(Element)nodeList.item(0);
System.out.println(el1.getAttribute("att1"));
*/
Node node0=nodeList.item(0);
strReturn=node0.getFirstChild().getNodeValue();
}catch(Exception e){
strReturn=e.toString().replaceAll("\n", "");
}
return strReturn;
}
public String getElementContent(String argfather,String son){
String strReturn="";
NodeList nodeList=doc.getElementsByTagName(argfather);
Node node0=nodeList.item(0);
NodeList objChildNodeList=node0.getChildNodes();
for(int i=0;i<objChildNodeList.getLength();i++){
Node temp=objChildNodeList.item(i);
//System.out.println(temp.getNodeValue());
if(son.equals(temp.getNodeName())){
strReturn=temp.getFirstChild().getNodeValue().trim();
break;
}
}
return strReturn;
}
public boolean addElement(String argFather,String argChild){
boolean result=false;
NodeList nodeList=doc.getElementsByTagName(argFather);
Node node0=nodeList.item(0);
Element element = doc.createElement("mov");
//element.setAttribute("movie", "mov1.swf");
//element.setAttribute("Text","txt1.swf");
//element.setAttribute("info","");
element.setNodeValue("phenix");
//node0.appendChild(element);
Node nodeTest=node0.appendChild(element);
System.out.println(nodeTest.getNodeName());
return result;
}
public static void main(String[] args){
XmlProcessor xml=new XmlProcessor("D:\\_eclipse2\\MBASite200812291326\\src\\DBCfg\\FilePath.xml");
//xml.addElement("TopCount","mov");
System.out.println(xml.getElementContent("SavePath"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<path>
<SavePath>/userfiles/fileUpload</SavePath>
<TempSavePath>/userfiles/fileUpload/temp</TempSavePath>
</path>
</xml-body>
out:
/userfiles/fileUpload