SAXReader 解析xml「建议收藏」

SAXReader 解析xml「建议收藏」使用SAXReader解析xmlSAXReader的read方法有几种不同的重载,大概包括以下几种1.read(Filefile) 传入文件对象2.read(InputSourcein)传入InputSource3.read(InputStreamin) 传入流……其实最终的格式都是转换为InputSource的格式packageD

大家好,又见面了,我是你们的朋友全栈君。使用SAXReader解析xml

SAXReader的read方法有几种不同的重载,大概包括以下几种
1.read(File file)  传入文件对象
2.read(InputSource in) 传入InputSource
3.read(InputStream in)  传入流
……
其实最终的格式都是转换为InputSource的格式

package Document;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

public class SAXRead {


/**
* 传入xml字符串
* @param xml
* @return
* @throws DocumentException
*/
private static Document paseXmlStr2Document(String xml) throws DocumentException{

SAXReader reader = new SAXReader();
Document doc = reader.read(new ByteArrayInputStream(xml.getBytes()));
return doc;
}
/**
* 传入xml文件路径
* @param filePath
* @return
* @throws DocumentException
* @throws FileNotFoundException
*/
private static Document paseFileStr2Document1(String filePath) throws DocumentException, FileNotFoundException{

SAXReader reader = new SAXReader();
File xmlFile = new File(filePath);
return reader.read(xmlFile);
}
/**
* 传入xml文件路径
* @param filePath
* @return
* @throws DocumentException
* @throws FileNotFoundException
*/
private static Document paseFileStr2Document(String filePath) throws DocumentException, FileNotFoundException{

SAXReader reader = new SAXReader();
File xmlFile = new File(filePath);
FileInputStream fis = new FileInputStream(xmlFile);
return reader.read(fis);
}

public static void main(String[] args) {

try {

Document doc = paseFileStr2Document1(“D:/s.xml”);
Element root = doc.getRootElement();
//打印根节点下某节点名对应的节点值
System.out.println(root.elementText(“节点名”));
//打印根节点的名字
System.out.println(root.getName());
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (DocumentException e) {

e.printStackTrace();
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150140.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月17日 下午11:18
下一篇 2022年6月17日 下午11:36


相关推荐

  • 2分钟规则

    怕什么真理无穷,进一步有近一步的欢喜每日语录:当你纠结要不要做一件事的时候,那就去做~开头聊几句1、又是一段时间没有输出内容了,有时候有点时间,自己懒不想动2、工作和生活,工作为了生活,…

    2022年3月1日
    44
  • 详解Java拦截器

    详解Java拦截器Interceptorsareusedtoimplementcross-cuttingconcerns,suchaslogging,auditing,andsecurity,fromthebusinesslogic.InJavaEE5,InterceptorswereallowedonlyonEJBs.InJavaEE6,Interceptorsbecameanewspecificationofitsown,abstracte.

    2022年5月2日
    92
  • 教程-经典Delphi教程网

    教程-经典Delphi教程网有理想+志同道合的人+取长补短去协同工作=完美团队一流的项目+三流的执行者=垃圾项目三流的项目+一流的执行者=完美项目自己公司网址:http://www.kaideruixin.icoc.cc/电脑管家误报提交地址:http://bbs.guanjia.qq.com/forum.php?mod=forumdisplay&fid=63本本驱动网站:http://www….

    2022年6月26日
    28
  • 刘权:“90后”讯飞星火大模型领头雁

    刘权:“90后”讯飞星火大模型领头雁

    2026年3月14日
    2
  • 谷歌打开微信定位服务器地址,使用Chrome修改user agent模拟微信内置浏览器

    谷歌打开微信定位服务器地址,使用Chrome修改user agent模拟微信内置浏览器很多时候,我们需要模拟微信内置浏览器,今天教大家用chrome简单模拟。如图设置:F12或者右键审查元素进入开发者模式,点击Emulation,然后点击Network,把Spoofuseragent改成Other,并把下面的带复制进去,有三句,大家自己尝试。回车然后刷新页面即可。Mozilla/5.0(iPhone;CPUiPhoneOS5_1likeMacOSX)Appl…

    2022年6月1日
    60
  • win10开始键没反应解决方法「建议收藏」

    win10开始键没反应解决方法「建议收藏」win10开始键没反应解决方法具体方法如下:1、打开运行窗口。windows7系统:通过“开始”菜单进入。点击“开始”菜单,从打开的菜单中依次点击“所有程序”>“附件”>“运行”来打开“运行”窗口。windows10系统:右击屏幕左下角win标志,在弹出的菜单中找到“运行”,点击进入运行窗口2、在搜索窗口输入“regedit”,打开注册表编辑器。3、在在注册表“HKEY_CLASSES_ROOT”主键下找到“lnkfile”字符串值项。打开它。4、在右侧右击它,会出现一个菜单

    2022年5月4日
    48

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号