java读取xml_JAVA读取XML所有内容

packagecom.word;importjava.util.Iterator;importjava.io.BufferedReader;importjava.io.File;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.MalformedURLException;import org.dom4j.*;importorg.dom4j.io.SAXReader;/*** 读取xml 所有内容

*@authorAdministrator

**/

public classXmlRead {static StringBuffer sBuilder = newStringBuffer();public static void main(String[] args) throwsIOException {

XmlRead xml= newXmlRead();

String path= "d:\\temp\\pzhd1389682365500.xml";

String xmlstr=xml.xmlToStr(path);

System.out.println("-----------end :");

System.out.println(xmlstr);

}/*** 读取xml文件格式+内容

*@paramfilePath

*@return

*/

publicString xmlToStr(String filePath){

String path=filePath;

Document document= null;try{

document=read(path);

}catch(MalformedURLException e) {

e.printStackTrace();

}catch(DocumentException e) {

e.printStackTrace();

}

Element root=getRootElement(document);if (root == null) {

System.out.print("没有获取到root节点");return null;

}//获取XML文档的编码格式

String encString =document.getXMLEncoding();

sBuilder.append("<?xml version=\"1.0\" encoding=\"" +encString+ "\">\n");

sBuilder.append(elementText(root, attText(root),0));

String ss= getIterator(root, 0) + "" + root.getName() + ">";

sBuilder.append(ss);returnsBuilder.toString();

}/*** 递归节点

*

* @description

*@paramelement

*@paramlvl

* 层级

*@return

*/

private static String getIterator(Element element, intlvl) {

lvl+= 1;//StringBuffer sBuilder = new StringBuffer();

for (Iterator i =element.elementIterator(); i.hasNext();) {

Element e=(Element) i.next();

sBuilder.append(elementText(e, attText(e), lvl));

getIterator(e, lvl);int count =e.nodeCount();if (count > 0) {for (int j = 0; j < lvl; j++) {

sBuilder.append(" ");

}

}

sBuilder.append(element.getStringValue());//文本值

sBuilder.append("" + e.getName() + ">\n");//System.out.println("========================>"+element.getStringValue());

}returnsBuilder.toString();

}/*** 获取当前节点的属性的值的字符串

*

* @description

*@paramelement

* 当前节点

*@return

*/

private staticString attText(Element element) {

String str= " ";for (int i = 0; i < element.attributeCount(); i++) {

Attribute attribute=element.attribute(i);

str+= attribute.getName() + "=\"" + attribute.getValue() + "\" ";

String ss=attribute.getText();

}returnstr;

}/*** 获取当前Element的文本值

*

* @description

*@paramelement

* 当前Element节点

*@paramtext

* 属性值

*@paramlvl

* 层级

*@return

*/

private static String elementText(Element element, String text, intlvl) {

String str= "";for (int i = 0; i < lvl; i++) {

str+= " ";

}

str+= "

str+=text;

}int count =element.nodeCount();if (count == 0) {return str += ">";

}return str += ">\n";

}/***

* @description 读取XML文件

*@paramfile

* XML文件路径,包含文件名

*@returnDocument 文档

*@throwsMalformedURLException

*@throwsDocumentException*/

public static Document read(String file) throwsMalformedURLException,

DocumentException {

SAXReader reader= newSAXReader();

Document document= reader.read(newFile(file));returndocument;

}/*** 获取Document文档的root节点

*

*@paramdocument

*@return

*/

public staticElement getRootElement(Document document) {returndocument.getRootElement();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值