版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012099869/article/details/78689148
一、写在前面:
- Dom 即 Document Object Model (文档对象模型),为 HTML、XML 设定的应用程序接口规范;
- 定义了文档的逻辑结构和访问、操作文档的方式;
- 不同环境应用程序访问时由各自语言实现具体的实现,如 Java(org.w3c.dom),ECMAScript;
- 以 OMG IDL 方式定义接口规范
二、DOM Core
未完待续,详情见文档。
三、Java (org.w3c.dom) 操作 XML 方式
test.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath*:properties/*.properties" />
<context:component-scan base-package="com.test.wlm" />
<bean id="myBean" destroy-method="close">
<property name="username" value="wlmmm" />
<property name="signature" value="这个少年很酷" />
</bean>
</beans>
解析 test.xml 代码如下:
@Test
public void xmlParseTest() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("/Users/wengliemiao/Downloads/test.xml");
Element root = doc.getDocumentElement();
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i ++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
System.out.println(ele.getTagName());
if (ele.getNodeName().equals("bean")) {
NodeList propertyList = ele.getChildNodes(); // 获取子节点
for (int j = 0; j < propertyList.getLength(); j ++) {
Node n = propertyList.item(j);
if (n instanceof Element) {
Element property = (Element) n;
String name = property.getAttribute("name");
String value = property.getAttribute("value");
System.out.println("\t" + name + ": " + value);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
输出结果为:
相关术语:
1.OMG(Object Management Group) 对象管理组织:
成立于1989年,作为一个非营利性组织,集中致力于开发在技术上具有先进性、在商业上具有可行性并且独立于厂商的软件互联规范。
2.CORBA(Common Object Request Broker Architecture) 通用对象请求代理体系结构:
OMG 组织制定的一种标准的面向对象应用程序体系规范。其核心部分是对象请求代理ORB(Object Request Broker)。
3.IDL(Interface Description Language) :接口描述语言,CORBA规范的一部分。是一种说明性语言,类似于C++。描述面向对象系统开发所遵循的接口与实现相分离的基本原则。
相关文档:
What is the Document Object Model?
Document Object Model Core