string to xml java_Java String to XML - Parse String to XML DOM Example - 入门小站-rumenz.com
在Java中,XML用org.w3c.dom.Document对象表示。 在本XML教程中,我们将学习-将XML string to XML转换string to XML文档
将XML文件内容转换为XML文档
1) Convert String to XML Document
要将XML字符串转换为XML Dom ,我们需要以下类:
javax.xml.parsers.DocumentBuilder :定义API,以从来自各种输入源的XML内容中获取XML DOM Document实例。 这些输入源是InputStreams,Files,URL和SAX InputSources。
javax.xml.parsers.DocumentBuilderFactory :定义一个工厂API,使应用程序能够获取解析器( DocumentBuilder ),该解析器从XML内容生成DOM对象树。
org.w3c.dom.Document :它表示整个XML DOM。 从概念上讲,它是文档树的根,并通过工厂方法提供对文档数据的访问,直到树的下层。
java.io.StringReader :从字符串内容创建流。 DocumentBuilder使用此流读取XML内容进行解析。package com.howtodoinjava.demo;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class StringtoXMLExample
{
public static void main(String[] args)
{
final String xmlStr = "" +
"" +
" Lokesh Gupta" +
"
Author" +
"" +
"" +
" Brian Lara" +
"
Cricketer" +
"" +
"";
//Use method to convert XML string content to XML Document object
Document doc = convertStringToXMLDocument( xmlStr );
//Verify XML document is build correctly
System.out.println(doc.getFirstChild().getNodeName());
}
private static Document convertStringToXMLDocument(String xmlString)
{
//Parser that produces DOM object trees from XML content
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//API to obtain DOM Document instance
DocumentBuilder builder = null;
try
{
//Create DocumentBuilder with default configuration
builder = factory.newDocumentBuilder();
//Parse the content to Document object
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
return doc;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
//Output:
employees
2) Convert XML file to XML Document
要从XML文件获取XML dom ,而不是将XML字符串传递给DocumentBuilder ,而是传递文件路径以使解析器直接读取文件内容。
我们有一个具有XML内容的employees.xml文件,我们将阅读以获取XML文档。
Lokesh Gupta
Author
Brian Lara
Cricketer
package com.howtodoinjava.demo;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class StringtoXMLExample
{
public static void main(String[] args)
{
final String xmlFilePath = "employees.xml";
//Use method to convert XML string content to XML Document object
Document doc = convertXMLFileToXMLDocument( xmlFilePath );
//Verify XML document is build correctly
System.out.println(doc.getFirstChild().getNodeName());
}
private static Document convertXMLFileToXMLDocument(String filePath)
{
//Parser that produces DOM object trees from XML content
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//API to obtain DOM Document instance
DocumentBuilder builder = null;
try
{
//Create DocumentBuilder with default configuration
builder = factory.newDocumentBuilder();
//Parse the content to Document object
Document doc = builder.parse(new File(filePath));
return doc;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
//Output:
employees
string to xml java_Java String to XML - Parse String to XML DOM Example - 入门小站-rumenz.com相关推荐
- The maximum string content length quota (8192) has been exceeded while reading XML data
原文: The maximum string content length quota (8192) has been exceeded while reading XML data 问题场景:在我们 ...
- cxf javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)
文章目录 1. 现象 2. 解决方案1 3. 解决方案2 1. 现象 CXF 在和xercesImpl.jar冲突的时候,抱错javax.xml.parsers.DocumentBuilderFact ...
- 解决javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V异常
文章目录 异常: 不同jar包的多xml解析器冲突 解决 其他 异常: java.lang.AbstractMethodError:javax.xml.parsers.DocumentBuilderF ...
- AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
文章目录 问题描述 问题原因 解决方案 问题描述 在spring boot 2.2.2项目中,使用mybatis-spring-boot-starter,引入如下: <dependency> ...
- Parse error in application web.xml file at jndi:/localhost/ipws/WEB-INF/web.xml
严重: Parse error in application web.xml file at jndi:/localhost/ipws/WEB-INF/web.xml java.lang.NoSuch ...
- 【Pandas】 ValueError: Unable to parse string “2,00“ at position 3769
问题: ValueError: Unable to parse string "2,00" at position 3769 在使用 pandas 对 dataframe 的 Ob ...
- Java的API(Scanner\Object\Date区\Math\Random\Arrays\String区\regex\包装类...)一般是parse(String)
Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JDK中提供给我们使用的类,这些类将底层的代码实现封装了 ...
- python xml解析dom_如何解析python中表示xml.dom.minidom节点的字符串?
我有一个使用xml.dom.minidom创建的nodes xml.dom.Node对象的集合.通过使用Node对象的toxml()方法将它们转换为字符串,我将它们(单独)存储在数据库中. 问题是,有 ...
- XML与web开发-01- 在页面显示和 XML DOM 解析
前言: 关于 xml 特点和基础知识,可以菜鸟教程进行学习:http://www.runoob.com/xml/xml-tutorial.html 本系列笔记,主要介绍 xml 在 web 开发时需要 ...
最新文章
- java 将本地图片批量上传到oss服务
- 4G EPS 中的小区选择
- NanoPi NEO Air使用十三:使用自带的fbtft驱动点亮SPI接口TFT屏幕,ST7789V,模块加载的方式
- I Hate It(线段树基础)
- nginx 支持php-fpm,nginx php-fpm安装配置以支持PHP
- Matlab C混合编程
- Java黑皮书课后题第6章:*6.4(反序显示一个整数)使用下面的方法体编写方法,反序显示一个整数…例如reverse(3456)返回6543,编写一个测试程序,提示用户输入一个整数,然后显示它的反序
- 关于结构体里面结构体的申明和使用
- echarts地图在ie浏览器上不显示
- 百度关键词排名查询源码_章丘百度霸屏总部,关键词排名腾沃云
- 【转】eclipse中egit插件使用
- linux 父子进程结束,Linux下让父进程结束后,子进程自动结束
- 第七章:在Spark集群上使用文件中的数据加载成为graph并进行操作(3)
- python如何获取百度搜索结果的真实URL
- VisualStudio2013内置SQLServer入门(二)--增删改查
- 2021-2025年中国制冷机行业市场供需与战略研究报告
- java实现rabbitmq路由模型(routing/topic queues), 生产者 消费者 交换机 消息队列
- ENVI实验教程(8)实验八、高光谱与光谱分析
- 通信系统中语音信号的仿真分析
- html+js+Jquery
热门文章
- 《京东峰值系统设计》读后感
- 中国人工智能学会通讯——艺术与人工智能的明天——人机信任合作 一、人工智能在艺术上的发展...
- Ubuntu Server上的LVM配置
- WEB前端性能优化小结
- VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径
- [iOS Animation]-CALayer 图层几何学
- [分享]组织机构图控件
- 关于 MySQL LEFT JOIN 你可能需要了解的三点
- 工信部:中国4G基站规模超200万个 4G用户数破5亿
- 易宝典——玩转O365中的EXO服务 之三十六 为软删除邮箱启用就地保留