Java读取xml数据

xml中的代码
<?xml version="1.0" encoding="UTF-8"?> <books id="aaa">
<book id="1">
<name>书名1</name>
<price>100</price>
</book>
<book id="2">
<name>书名2</name>
<price>300</price>
</book>
</books>
java中的代码
import java.io.File;
import java.io.IOException; import javax.lang.model.element.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.Node; import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; public class ReadXml { public static void main(String[] args) { try {
//java读取xml的方法。DOM方式
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("NewFile.xml"));
//获取文档根元素
org.w3c.dom.Element root=document.getDocumentElement();
//输出根元素id的值
System.out.println(root.getAttribute("id"));
//获取子级元素
NodeList list= root.getElementsByTagName("book");
//循环输出xml标签元素的值
for (int i = 0; i < list.getLength(); i++) {
org.w3c.dom.Element book=(org.w3c.dom.Element)list.item(i);
System.out.println("--------");
System.out.println("id="+book.getAttribute("id"));
//获取所有子节点
NodeList list1=book.getChildNodes();
//循环输出子节点中的元素内容
for (int j = 0; j < list1.getLength(); j++) {
org.w3c.dom.Node child=(org.w3c.dom.Node)list1.item(j);
//去掉空白节点
if(child instanceof org.w3c.dom.Element){
System.out.println(child.getNodeName()+"标签内容是"+child.getTextContent());
}
} //读取子节点中元素的值,都要重新创建一个element对象
/*org.w3c.dom.Element name=(org.w3c.dom.Element) book.getElementsByTagName("name").item(0);
System.out.println("name="+name.getTextContent());*/
}
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
} catch (SAXException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
} }
运行结果
aaa
--------
id=1
name标签内容是书名1
price标签内容是100
--------
id=2
name标签内容是书名2
price标签内容是300
Java读取xml数据的更多相关文章
- 使用Java读取XML数据
---------------siwuxie095 工程名:TestReadXML 包名:com.siwuxie095.xml 类名:ReadXML.java 打开资源管理器,在工程 TestRead ...
- Java中XML数据
Java中XML数据 XML解析——Java中XML的四种解析方式 XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解 ...
- wcf序列化大对象时报错:读取 XML 数据时,超出最大
错误为: 访问服务异常:格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出 错: request.InnerException 消息是“反序 ...
- java 读取XML文件作为配置文件
首先,贴上自己的实例: XML文件:NewFile.xml(该文件与src目录同级) <?xml version="1.0" encoding="UTF-8&quo ...
- java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file” .
背景:java读取xml文件,xml文件内容只有“<?xml version="1.0" encoding="UTF-8"?>”一行 java读取该 ...
- InnerException 消息是“反序列化对象 属于类型 *** 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。(注意细节)
WEB站点在调用我们WCF服务的时候,只要传入的参数过长,就报如下错误: 格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: formD ...
- java读取XML文件的四种方式
java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...
- JAVA读取XML文件并解析获取元素、属性值、子元素信息
JAVA读取XML文件并解析获取元素.属性值.子元素信息 关键字 XML读取 InputStream DocumentBuilderFactory Element Node 前言 最 ...
- Web Service 或 WCF调用时读取 XML 数据时,超出最大字符串内容长度配额(8192)解决方法
1.调用服务时服务 当我们使用 Web Service 或 WCF 服务时,常把读取的数据转化为string类型(xml格式),当数据量达到一 定数量时,会出现以下异常: 错误:格式化程序尝试对消息反 ...
随机推荐
- 解决maven的“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
需要添加一个插件,在build标签中添加 <plugins> <plugin> <groupId>org.apache.maven.plugins</grou ...
- Python3基础 print 自带换行功能
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 【leetcode❤python】101. Symmetric Tree
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- UVA 11584 一 Partitioning by Palindromes
Partitioning by Palindromes Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- Cheatsheet: 2014 08.01 ~ 08.31
Web Slow Server? This is the Flow Chart You're Looking For A Strolll Through Node: Introduction .NET ...
- C# 返回Foreach集合
IEnumerable<DataRow> DetailRows() { foreach (DataRow dr in EditData.Tables[tb_ ...
- CP,SCP 命令(包括windows与linux用xshell互传)
linux之cp/scp命令+scp命令详解 名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... direct ...
- Using Post-Form Trigger In Oracle Forms
Post-Form trigger in Oracle Forms fires during the Leave the Form process, when a form is exited. ...
- R: NULL, NA, and NaN
NaN (“Not a Number”) means 0/0 NA (“Not Available”) is generally interpreted as a missing value and ...
- 基于Microchip单片机的触摸感应技术
Microchip提供两种电容式触摸感应解决方案,一种为张驰振荡器方式,即通过检测触摸感应电容充放电的频率变化,来检测是否有键按下,根据单片机集成的硬件资源不同,另一种通过Microchip单片机集成 ...