java jdom xml 解析_基于jdom将指定xml文件读取解析为字符串文本内容java代码示例...

一、前言

通过jdom依赖包将指定xml文件加载解析为字符串文本内容的JAVA代码示例。

二、代码示例import java.io.File;@b@import java.io.IOException;@b@import java.io.StringWriter;@b@@b@import org.jdom.input.SAXBuilder;@b@import org.jdom.output.Format;@b@import org.jdom.output.XMLOutputter;@b@@b@public class XmlToStringUtil {@b@@b@ //将xml文件解析为内存文件document@b@ public static org.jdom.Document xml2Document(String filepath){@b@ org.jdom.Document document=null;@b@ try {@b@ SAXBuilder reader = new SAXBuilder(); @b@ document=reader.build(new File(filepath));@b@} catch (Exception e) {@b@ e.printStackTrace();@b@}@b@ return document;@b@ }@b@ @b@ //将xml文件解析为字符串@b@ public static String xml2String(String filepath){@b@ org.jdom.Document document=xml2Document(filepath);@b@ Format format =Format.getPrettyFormat();    @b@ format.setEncoding("UTF-8");//设置编码格式 @b@ @b@ StringWriter out=new StringWriter();  //输出对象@b@ XMLOutputter outputter =new XMLOutputter(); @b@ try {@b@outputter.output(document,out);@b@ } catch (IOException e) {@b@e.printStackTrace();@b@ } @b@ return out.toString();@b@ }@b@ @b@ public  static  void  main(String[] args){@b@     System.out.println(XmlToStringUtil.xml2String("Y:/apache-maven2/maven2/abbot/abbot/0.12.3/abbot-0.12.3.pom"));@b@ }@b@@b@}

控制台打印结果<?xml  version="1.0" encoding="UTF-8"?>@b@@b@  4.0.0@b@  abbot@b@  abbot@b@  Abbot@b@  0.12.3@b@

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值