1简介:XML,可扩展的标记性语言,提供了一套跨平台,跨网路、跨程序的语言 的数据描述方式,使用xml,可以实现数数据交换,系统配置,内容管理 等常见功能
XML与HTML 都是由SGML,发展而来,区别在于:Html中的元素都是固定的,以显示为主,XML中的语言标记是用户自定义的,主要以保存数据为主。
写两段代码对比
xml:
<?xml version="1.0" encoding="GB2312"?>
<addresslist>
<linkman>
<name>liwei</name>
<id>0520</id>
<phone>1314</phone>
</linkman>
</addresslist>
html:
<html>
<head>
<title>www.ailiwei.com</title>
</head>
<body>
<li>洛阳,牡丹之都!</li>
</body>
</html>
对比结论:xml 的标签由用户自己定义,html是内置的
注意:在进行xml声明时其中的三个属性必须按照固定的顺序编写,否则报错,version版本 encoding文字编码 standalone:此xml文件是否独立运行
2XML解析
2.1 xml文件更多的用于描述信息,利用程序取按照其中元素定义的名称取出对应的内容,这样的操作称为XML解析,
在xml解析中为W3C定义了SAX 和DOM 两种解析方式
2.2 过程:首先由xml分析器对xml文档进行分析,然后应用程序通过分析器提供的额SAX或DOM接口实现对xml文档的访问。
2.3DOM解析操作
1)在应用程序中,基于DOM(文档对象模型)的XML分析器将一个XML文档转换成一个对象的模型集合(DOM树),应用程序通过对这个模型的操作来实现对XML的操作。
2)在DOM解析中有四个核心的操作接口
Document:代表整个xml文档,表示整颗DOM树的根
Node:Dom操作的核心接口有一大部分都是继承自Node
Nodelist:表示一个节点的集合,一般用于表示有顺序关系的一组节点
NameNodeMap:表示一组节点的和其唯一名称对应的一一对应关系
举例:在D盘里新建一个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<addresslist>
<name>李小兔</name>
</addresslist>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">package mjc;</span>
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test{
public static void main(String args[]){
//建立DocumentBuilderFactory,用于取得DocumentBuilder
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder =null;
try {
builder=factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//定义Document接口对象,通过DocumentBuilder类进行DOM树的转换操作
Document doc=null;
try {//读取指定路径 的xml,将xml文档读取到内存中
doc=builder.parse("D:"+File.separator+"demo.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//查找name的节点
NodeList nl=doc.getElementsByTagName("name");
//输出NodeList中第一个子节点中文本节点的内容
System.out.print("姓名:"+nl.item(0).getFirstChild().getNodeValue());
}
}
举例:生成文件XML文件到D盘
package mjc;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class XmlOut {
public static void main(String[] args) {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder =null;
try {
builder=factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document doc=null;
doc=builder.newDocument();
//1建立各个操作节点
Element addresslist=doc.createElement("addresslist");
Element linkman=doc.createElement("linkman");
Element name=doc.createElement("name");
Element email=doc.createElement("email");
//2设置节点内容
name.appendChild(doc.createTextNode("李小兔"));
email.appendChild(doc.createTextNode("123@qq.com"));
//3设置节点关系
linkman.appendChild(name);
linkman.appendChild(email);
addresslist.appendChild(linkman);
doc.appendChild(linkman);
//4输出文档到文件中
TransformerFactory tf=TransformerFactory.newInstance();
Transformer t=null;
try {
t=tf.newTransformer();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置编码
t.setOutputProperty(OutputKeys.ENCODING, "utf-8");
DOMSource source =new DOMSource(doc);
//设置输出路径
StreamResult result =new StreamResult(new File("d:"+File.separator+"output.xml"));
//5输出
try {
t.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
</pre><p><span style="font-size:24px;"><strong>3浅谈XML的跨平台传输</strong></span></p><p><span style="font-size:18px;">转自:http://blog.youkuaiyun.com/littletigerat/article/details/44672557</span></p><p></p><p><span style="font-size:18px;"><strong>3.1先说说平台: </strong></span></p><p><span style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><span style="font-size:18px;">跨平台概念是软件开发中一个重要的概念,即不依赖于操作系统,也不依赖硬件环境。</span></span></p><p></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><span style="font-size:18px;">操作系统有哪些,如:Window、Unix、Linux、Mac等。</span></p><p align="left" style="font-family: Arial; line-height: 26px;"></p><p align="left" style="font-size:18px; font-family: Arial; line-height: 26px;"><strong>3.2 跨平台包括哪些方面?</strong></p><p align="left" style="font-size:18px; font-family: Arial; line-height: 26px;">1. 字符;</p><p align="left" style="font-size:18px; font-family: Arial; line-height: 26px;">2. 文件;</p><p align="left" style="font-size:18px; font-family: Arial; line-height: 26px;">3. 通信协议;</p><p align="left" style="font-size:18px; font-family: Arial; line-height: 26px;">4. 程序; </p><p align="left" style="font-size:18px; font-family: Arial; line-height: 26px;"> Java语言编写的程序,应该属于:程序层面的跨平台。</p><p align="left" style="font-size:18px; font-family: Arial; line-height: 26px;"> XML应该属于文件、通信协议方面的,跨平台。</p><p align="left" style="font-family: Arial; line-height: 26px;"></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong>3. 3 理解字符跨平台,是理解XML跨平台的第一步;</strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">字符编码标准有ASCII、GB2312、GBK、Big5、Unicode、UTF8等。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">具体说明:</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">“汉”字的UTF8编码是E6B189。也就是说,只要告诉计算机系统,这个字是UTF8编码,</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">如果该字符编码是E6B189,那么我就解析为<strong>“汉”字的“汉”</strong>。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">反之,如果告诉计算机该字是<strong>“汉”字的“汉”,</strong>则将该字符编码为E6B189,进行存储。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"> </p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">也就是说,<strong>只要计算机系统,只要遵循字符编码规范,是容易理解和实现,字符层面的跨平台。</strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"> </p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong>所以说,字符编码标准,这是计算机发展,最基础层面的标准。</strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong><span style="color: red;">在不同的操作系统,不同的硬件平台,只要基于相同的编码标准,字符是可以跨平台。</span></strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong> </strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">3.4 XML是属于文件、通信协议方面的跨平台。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong></strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong>一、文件层面的跨平台:</strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong>首先,先看看XML长得啥模样?</strong></p><div style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; background-image: initial; background-attachment: initial; background-color: rgb(245, 245, 245); background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><p align="left"><?xml version="1.0" encoding="ISO-8859-1"?></p><p align="left"><note></p><p align="left"><to>George</to></p><p align="left"><from>John</from></p><p align="left"><heading>Reminder</heading></p><p align="left"><body>Don't forget the meeting!</body></p><p align="left"></note></p></div><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"> </p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong>接着,把握文件层面实现跨平台的逻辑推理要点:</strong></p><ol start="1" type="1" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><li><strong><span style="color: red;">文件、文档的跨平台,必然通过字符、文字的来实现</span></strong>;</li><li><strong><span style="color: red;">在当今多语言的计算机环境下,必然涉及到编码转化的问题,例子中</span><span style="color: red;">xml</span><span style="color: red;">文档中的</span><span style="color: red;">encoding</span><span style="color: red;">属性,就解决了编码识别与转化的问题</span></strong>;</li><li style="color: red;"><strong>文本,偶的理解,就是文字、字符的意思。</strong></li></ol><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong><span style="color: red;">文本就是相对图像、音频、视频等而言</span></strong>。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">文本可以包括文字信息,以及文字表现信息,也就是文字装饰信息,如:字体、字号、下划线、斜体、图形、符号或特殊字符及特殊打印格式等。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">纯文本,就是没有任何文字表现信息,没有任何文字装饰信息,没有任何字体、字号、下划线、斜体、图形、符号或特殊字符及特殊打印格式等信息。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">说白了,就是一个光文字信息,俗称“裸字”信息,也就是纯字符。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"> </p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">【结论】</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong><span style="color: red;">既然是“裸字”信息,纯字符,再加上当前的字符,使用的何种编码标准,当然可以</span></strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong><span style="color: red;">跨平台。</span></strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong>二、通信协议层面的跨平台:</strong></p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">XML通信协议,基于HTTP协议上的扩展。</p><p align="left" style="font-size:18px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong>因为HTTP协议,是跨平台的,所以,基于XML通信协议,是跨平台的。</strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong><span style="font-size:24px;"></span></strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><strong><span style="font-size:24px;">4跨平台传输,XML 和JSON的比较</span></strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><span style="font-size:18px;"></span><div>作者:Milo Yip链接:http://www.zhihu.com/question/25636060/answer/31257498来源:知乎<div class="zm-editable-content clearfix">我只提一个使用上的因素:JSON的结构更容易<strong>映射</strong>至一般语言的数据结构。XML和JSON的主要组成成分:<ul><li>XML是element、attribute和element content。</li><li>JSON是object、array、string、number、boolean(true/false)和null。</li></ul>XML要表示一个object (指name-value pair的集合),最初可能会使用element作为object,每个key-value pair 用 attribute 表示:<div class="highlight"><pre><code class="language-text"><student name="John" age="10"/>
</code>
但如个某个 value 也是 object,那么就不可以当作attribute:
<student name="John" age="10">
<address>
<country>China</country>
<province>Guang Dong</province>
<city>...</city>
<district>...</district>
...
</address>
</student>
而JSON因为有object这种类型,可以自然地映射,不需考虑上述的问题,自然地得到以下的格式。
{
"name": "John",
"age" : 10,
"address" : {
"country" : "China",
"province" : "Guang Dong",
"city" : "..",
"district" : "..",
...
}
}
除此以外,
- XML需要选择怎么处理element content的换行,而JSON string则不须作这个选择。
- XML只有文字,没有预设的数字格式,而JSON则有明确的number格式,这样在locale上也安全。
- XML映射数组没大问题,就是数组元素tag比较重复冗余。JSON 比较易读。
- JSON的true/false/null也能容易统一至一般编程语言的对应语义。