Java 读写XML
package dome4jTest; import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator; import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; /**
* @author Young
* @version 1.0
*
*/
public class Dom4j { private static String file = "output.xml";
private static URL url; public static void main(String[] args) throws DocumentException, IOException {
createDocument(); url = new URL(
"http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=xml&ak=sXF8krbnX1LXbz0TpzivvOS2"); SAXReader reader = new SAXReader(); Document document = reader.read(url); Element root = document.getRootElement();
// System.out.println(root.getName());
// String arg0="results";
// String arg1="weather_data";
// System.out.println(root.element(arg0).element(arg1).element("date").getText());
//
// iterate through child elements of root
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if(!element.getName().equals("results"))
{
System.out.println("++++++++++++++++++++++++++");
continue;
} for(Iterator k = element.elementIterator(); k.hasNext();)
{
Element childElement = (Element) k.next();
if(!childElement.getName().equals("weather_data"))
{
System.out.println("==============================");
continue;
}
// System.out.println("get Name -->" + childElement.getName());
// System.out.println("get type -->" + childElement.getData());
// System.out.println("get type -->" + childElement.getText()); for(Iterator m = element.elementIterator(); m.hasNext();)
{
Element grandElement = (Element) m.next();
System.out.println("get Name -->" + grandElement.getName());
System.out.println("get Path-->" + grandElement.getPath());
System.out.println("get Text -->" + grandElement.getText());
System.out.println("get nodeCount -->" + grandElement.nodeCount());
System.out.println("get Node Type-->" + grandElement.getNodeType());
System.out.println("get Node Type-->" + grandElement.getNodeTypeName()); } } // iterate through attributes of root
for (Iterator j = element.attributeIterator(); j.hasNext();) {
Attribute attribute = (Attribute) j.next();
System.out.println("attribute name " + element.getName() + " ->" + attribute.getName());
System.out.println("attribute name " + element.getName() + " ->" + attribute.getPath()); }
} // iterate through child elements of root with element name "foo"
for (Iterator i = root.elementIterator("results"); i.hasNext();) {
Element results = (Element) i.next();
System.out.println("results -->" + results.getName());
System.out.println("results -->" + results.getData());
// iterate through attributes of root
for (Iterator j = results.attributeIterator(); j.hasNext();) {
Attribute attribute = (Attribute) j.next();
System.out.println("attribute name " + results.getName() + " ->" + attribute.getName());
System.out.println("attribute name " + results.getName() + " ->" + attribute.getPath()); }
} } public static Document createDocument() throws IOException {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"), format); Document document = DocumentHelper.createDocument();
Element root = document.addElement("root"); root.addAttribute("ClassA", "Test2");
root.addAttribute("ClassB", "Test2");
Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK")
.addText("James Strachan").addAttribute("Test1", "Values").addText("Test"); Element author2 = root.addElement("author").addAttribute("name", "Bob").addAttribute("location", "US")
.addText("Bob McWhirter"); Element author3 = root.addElement("author").addAttribute("name", "Jack").addAttribute("location", "US")
.addText("Jack Ma"); writer.write(document);
writer.close();
return document;
} }
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator; import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; /**
* @author Young
* @version 1.0
*
*/
public class Dom4j { private static String file = "output.xml";
private static URL url; public static void main(String[] args) throws DocumentException,
IOException {
createDocument(); url = new URL(
"http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=xml&ak=sXF8krbnX1LXbz0TpzivvOS2"); SAXReader reader = new SAXReader(); Document document = reader.read(url); Element root = document.getRootElement();
// System.out.println(root.getName());
// String arg0="results";
// String arg1="weather_data";
// System.out.println(root.element(arg0).element(arg1).element("date").getText());
//
// iterate through child elements of root
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if (!element.getName().equals("results")) {
System.out.println("++++++++++++++++++++++++++");
continue;
} for (Iterator k = element.elementIterator(); k.hasNext();) {
Element childElement = (Element) k.next();
if (!childElement.getName().equals("weather_data")) {
System.out.println("==============================");
continue;
}
// System.out.println("get Name -->" + childElement.getName());
// System.out.println("get type -->" + childElement.getData());
// System.out.println("get type -->" + childElement.getText()); for (Iterator m = element.elementIterator(); m.hasNext();) {
Element grandElement = (Element) m.next();
System.out.println("get Name -->" + grandElement.getName());
System.out.println("get Path-->" + grandElement.getPath());
System.out.println("get Text -->" + grandElement.getText());
System.out.println("get nodeCount -->"
+ grandElement.nodeCount());
System.out.println("get Node Type-->"
+ grandElement.getNodeType());
System.out.println("get Node Type-->"
+ grandElement.getNodeTypeName()); } } // iterate through attributes of root
for (Iterator j = element.attributeIterator(); j.hasNext();) {
Attribute attribute = (Attribute) j.next();
System.out.println("attribute name " + element.getName()
+ " ->" + attribute.getName());
System.out.println("attribute name " + element.getName()
+ " ->" + attribute.getPath()); }
} // iterate through child elements of root with element name "foo"
for (Iterator i = root.elementIterator("results"); i.hasNext();) {
Element results = (Element) i.next();
System.out.println("results -->" + results.getName());
System.out.println("results -->" + results.getData());
// iterate through attributes of root
for (Iterator j = results.attributeIterator(); j.hasNext();) {
Attribute attribute = (Attribute) j.next();
System.out.println("attribute name " + results.getName()
+ " ->" + attribute.getName());
System.out.println("attribute name " + results.getName()
+ " ->" + attribute.getPath()); }
} } public static Document createDocument() throws IOException {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"), format); Document document = DocumentHelper.createDocument();
Element root = document.addElement("note"); root.addAttribute("ClassA", "Test2");
root.addAttribute("ClassB", "Test2");
root.addComment("This is a comment");
Element author1 = root.addElement("author")
.addAttribute("name", "James").addAttribute("location", "UK")
.addText("James Strachan").addAttribute("Test1", "Values")
.addText("Test").addElement("Test").addAttribute("A", "B").addText("This is Test"); Element author2 = root.addElement("author").addAttribute("name", "Bob")
.addAttribute("location", "US").addText("Bob McWhirter"); Element author3 = root.addElement("author")
.addAttribute("name", "Jack").addAttribute("location", "US")
.addText("Jack Ma"); writer.write(document);
writer.close();
return document;
} }
Java 读写XML的更多相关文章
- Java 读写XML文件 API--org.dom4j
om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...
- Java读写XML代码示例
采用技术:dom4j 需要jar包:dom4j-1.6.1.jar 主类如下---- public class dom4jTestXML { /** * * 方法描述:读取XML文件示例 备 注 ...
- JAVA对XML文件的读写(有具体的代码和解析
XML 指可扩展标记语言(EXtensible Markup Language),是独立于软件和硬件的信息传输工具,应用于 web 开发的许多方面,常用于简化数据的存储和共享. xml指令 处理指令, ...
- java通过dom读写xml文件
java通过dom读写xml文件 要读的xml文件 <?xml version="1.0" encoding="GB2312"?><学生花名册 ...
- JAVA学习笔记 -- 读写XML
XML是一种可扩展标记语言 以下是一个完整的XML文件(也是下文介绍读写XML的样本): <? xml version="1.0" encoding="UTF-8& ...
- 【网络爬虫】【java】微博爬虫(四):数据处理——jsoup工具解析html、dom4j读写xml
之前提到过,对于简单的网页结构解析,可以直接通过观察法.手工写正则解析,可以做出来,比如网易微博.但是对于结构稍微复杂点的,比如新浪微博,如果还用正则,用眼睛一个个去找,未免太麻烦了. 本文介绍两个工 ...
- JAVA对XML文件的读写
XML 指可扩展标记语言(EXtensible Markup Language),是独立于软件和硬件的信息传输工具,应用于 web 开发的许多方面,常用于简化数据的存储和共享. xml指令处理指令,简 ...
- xml-mapping xml 与 java 对象转换映射框架,像 XStream 一样优雅地读写xml
xml xml 是 java 实现的 xml 框架. 希望以最优雅的方式进行 xml 和 java 之间的转换处理,一行代码搞定一切. 特点 对象的和 xml 的互相映射 支持注解 @Alias 指定 ...
- Java获取XML节点总结之读取XML文档节点
dom4j是Java的XML API,用来读写XML文件的.目前有很多场景中使用dom4j来读写xml的.要使用dom4j开发,需要下载导入dom4j相应的jar文件.官网下载:http://www. ...
随机推荐
- 51Nod--1015 水仙花数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1015 1015 水仙花数 基准时间限制:1 秒 空间 ...
- 小记:目标数组的长度不够。请检查 destIndex 和长度以及数组的下限。
异常:System.ArgumentException: 目标数组的长度不够.请检查 destIndex 和长度以及数组的下限.(不好意思忘记截图了) 发生异常的代码如下: var list = ne ...
- 线段树 HDU 3397
5种操作 具体看代码 #include<iostream> #include<stdio.h> #include<string.h> #include<alg ...
- CSS备忘录
1, 浮动框(float)会使得元素脱离文档流, 因此不占用文档空间; 因此当一个Div的子元素都为float时会导致该Div高度为0, 这一问题称为"高度塌陷" 为了解决这个问题 ...
- css-margin与百分数的关系
可以对元素的margin设置百分数,百分数是相对于父元素的width计算,不管是margin-top/margin-bottom还是margin-left/margin-right.(padding同 ...
- <<< 编程类开发工具
Java.开发工具 java运行环境JDK下载 1.6 →下载JDK1.6 1.7 →下载JDK1.7 简介:著名的跨平台开源集成开发环境(IDE).最初主要用来Java语言开发,Eclipse的本身 ...
- BZOJ3110: [Zjoi2013]K大数查询
喜闻乐见的简单树套树= =第一维按权值建树状数组,第二维按下标建动态开点线段树,修改相当于第二维区间加,查询在树状数组上二分,比一般的线段树还短= =可惜并不能跑过整体二分= =另外bzoj上的数据有 ...
- HTML5本地存储——IndexedDB(一:基本使用)
在HTML5本地存储——Web SQL Database提到过Web SQL Database实际上已经被废弃,而HTML5的支持的本地存储实际上变成了 Web Storage(Local Stora ...
- shell判断条件整理
1.字符串判断 str1 = str2 当两个字符串串有相同内容.长度时为真 str1 != str2 当字符串str1和str2不等时为真 -n str1 当字符串的长度大于0时为真(串非空) -z ...
- js自执行函数注意事项
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...