---------------------------------------------------------------------------------
前提条件
---------------------------------------------------------------------------------
导入jdom包
----------------------------------------------------------------------------------
JAVA类JDomParse
----------------------------------------------------------------------------------
package xmlparse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class JDomParse {
/**
* 解析方法
* @param upstr --父标签
* @param str --子标签
* @param xmlpath--文件路径
* @return
*/
public String getXml(String upstr,String str,String xmlpath){
SAXBuilder builder=new SAXBuilder(false);
String strContent="";
try {
Document doc=builder.build(xmlpath);
Element config=doc.getRootElement();
List list=config.getChildren(upstr);
for (Iterator iter = list.iterator(); iter.hasNext();) {
Element child = (Element) iter.next();
strContent=child.getChildTextTrim(str);
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strContent;
}
/**
* 测试方法
* @param arg
*/
public static void main(String[] arg){
String xmlpath="G://kh//gscx//src//hdjkConfig.xml";
String upstr="ftp";
String str="ip";
JDomParse jd=new JDomParse();
String jdxml=jd.getXml(upstr, str, xmlpath);
System.out.println(jdxml);
}
}
-------------------------------------------------------------------------------
xml文件hdjkConfig.xml
-------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<config>
<ftp>
<ip>10.253.34.14</ip>
<port>21</port>
<user>weblogic</user>
<pwd>weblogic</pwd>
</ftp>
</config>
---------------------------------------------------------------------------------
显示结果
---------------------------------------------------------------------------------
10.253.34.14