Crazy Programmer Homework will be released as a series what i had planed, today i will pull the first term:Crazy Programmer Homework One, and the Two, Three... will coming sooner
Today's Topic is Apache Xalan, i do not spend a lot of time to dipict Xalan's definition, history, usages and so on, i only have given a series of example java code to illustrate how to learn Xalan. However, i will start our Part One with two nouns' Distinguish:JAXB and JAXP
Part One: Deeply Learn XPath
1. the difference between JAXB and JAXP?
i was confused for quite a long while, actually its very zasy to make sense, just like the follow dipicted:
JAXP - Java API for XML Processing which used to process xml
JAXB - Java Architecture for XML Binding which used to bind xml with a java entity.
2. ApplyXPathJAXP
ApplyXPathJAXP is java class to demonstrate how to uses the XPath API in JAXP 1.3 to evaluate an XPath expression against an XML document and return the evaluation result in the specified type.
Through this example u can know some basic theroy about QName, Node, XPath, Transformer, ect.
Code Overview:
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression xpathExpr = xpath.compile(expression);
...
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Detail Code can be found from attached file.
3. XPathResolver
XPathResolver provides sample implementations of the NamespaceContext, XPathFunctionResolver and XPathVariableResolver interfaces described in the JAXP 1.3 XPath API. The sample demonstrates how these implementations can be used to to evaluate XPath expressions that contain extension functions and references to variables.
Code Overview:
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new MyNamespaceContext());
xpath.setXPathFunctionResolver(new MyFunctionResolver());
xpath.setXPathVariableResolver(new MyVariableResolver());
Object result = null;
try {
result = xpath.evaluate(EXPR, (Object)null, XPathConstants.NUMBER);
} catch (Exception e) {
e.printStackTrace();
}
The detail code can be found in attached file either.
4. ExtensionFunctionResolver
ExtensionFunctionResolver is a expand of XPathResolver, Apache Xalan add his owns implementation of NamespaceContext and XPathVariableResolver, ExtensionFunctionResolver demonstrates how to use the sample implementation of XPathFunctionResolver to evaluate XPath expressions containing Java or EXSLT extension functions.
Code Overview:
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new ExtensionNamespaceContext());
xpath.setXPathFunctionResolver(new XPathFunctionResolverImpl());
5. ApplyXPath
ApplyXPath uses the convenience methods in the Xalan-Java 2 specific XPathAPI to execute an XPath expression against an XML document and return the nodes (if any) it finds, just like the following line dipicted:
NodeIterator nl = XPathAPI.selectNodeIterator(doc, xpath);
this sample also demonstrates some basic method, for instance, use DocumentBuilderFactory create a Document:
InputSource inputSource = new InputSource(new FileInputStream(filename));
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
Document doc = dfactory.newDocumentBuilder().parse(inputSource);
XPath expression, if our xml as follow
<doc>
<addr>
<street>
<number value="203">3#</number>
<number value="204">3#</number>
<number value="205">3#</number>
<number>4#</number>
<number>5#</number>
<number>6#</number>
</street>
</addr>
</doc>
we can use the following expressions to iterator our xml's node
String xpath = "/doc/addr";
String xpath = "/doc/addr/street";
String xpath = "/doc/addr/street/number";
String xpath = "/doc/addr/street/number/@value";
as bellow code descripted, we also can iterator node's atrribute use '@'
6. ApplyXPathDOM
ApplyXPathDOM is very similar to the ApplyXPath sample, but it uses the API in the DOM Level 3 XPath Specification to execute an XPath expression against an XML document and return the nodes (if any) it finds.
Part Two: Use Dom accessing and manipulating XML
The XML DOM defines a standard way for accessing and manipulating XML documents. DOM presents XML as tree-structure, load all XML to memory, but its very useful method to parse XML, as usually, i planed to start this part with nouns distinguish.
1. what's the difference between Node and Element?
as the following pic depicted:
Node and Element are 2 interface under org.w3c.dom package, Element is subinterface of Node.
2. Use DOM create XML
(1). create a docuement
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XUtil.checkOutPath("foo.xml")));
(2). create a empty document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.newDocument();
(3). serializer document as stream
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.transform(new DOMSource(document), new StreamResult(new OutputStreamWriter(System.out)));
(4). serializer document to a string
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
serializer.transform(new DOMSource(document), new StreamResult(writer));
String xml = writer.toString();
3. Use Dom add element
(1). create root element
Document document = createEmptyDocument();
Element element = document.createElement(rootTagName);
document.appendChild(element);
(2). create common element
Document document = parent.getOwnerDocument();
Element child = document.createElement(tagName);
parent.appendChild(child);
- 大小: 1.7 KB
分享到:
相关推荐
Apache Thrift is an open source cross language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed ...
That environment will reflect the programmer's individuality just as forcefully as his or her hobbies, clothing, or haircut. However, if you're a Pragmatic Programmer, you'll share many of the ...
当在使用Nios II处理器与EPCS进行交互的过程中,可能遇到烧写错误,例如出现"Error: Error code: 8 for command: nios2-flash-programmer"。这种错误提示通常意味着在烧写操作过程中存在一些问题。下面详细解释了在...
程序员考试刷题Perguntas 团结 这是我为练习而制作的应用程序。 它有两种模式,练习和考试。 一半的问题来自官方来源,另一半来自书中。 我已经使这个应用程序尽可能接近考试形式。 ...您可以选择进行模拟测试、特定...
### Android-A Programmer's Guide知识点详解 #### 一、Android应用程序基础构建 在《Android-A Programmer's Guide》这部著作中,作者通过一系列实例深入浅出地介绍了Android应用开发的基础知识。本篇文章聚焦于...
Apache Thrift程序员指南以下示例中的源代码:Apache Thrift程序员指南 本书分为三个部分:第一部分-Apache Thrift概述对Apache Thrift及其体系结构的高级介绍。 这部分的例子非常有趣。 本部分还介绍了基本的Apache...
在这个背景下,"cmd-programmer-org:hpsdr p1协议的命令行程序员" 是一个针对TapR SDR项目中的特定工具,用于与High Performance Software Defined Radio (HPSDR)系统的P1协议进行交互。 HPSDR是一个开源的、高...
【pelican-programmer-theme: 鹈鹕主题详解】 在Web开发领域,个性化和美观的博客主题对于提升网站用户体验至关重要。"pelican-programmer-theme"就是这样一个专为[鹈鹕]内容管理系统设计的主题,旨在为程序员们...
【标题】:“Daily-Programmer-Challenges:http”指的是在编程社区Reddit上的“Daily Programmer”子论坛上发布的关于HTTP协议的编程挑战。 【描述】:这个挑战集合是为那些寻求提升编程技能并保持头脑活跃的程序员...
M-x package-install RET programmer-dvorak 然后将其添加到您的 emacs 配置文件中。 (require 'programmer-dvorak) 用法 `programmer-dvorak' 将被注册为 Emacs 中的标准输入法,选择它: M-x set-input-...
GD32 All-In-One Programmer V2.0.1.13749是由兆易创新推出的官方软件工具,专门针对其系列MCU设计,旨在简化并优化这些过程。这款软件基于芯片内部的Bootloader,提供了一种高效且便捷的途径,来管理和操控MCU的...
Title: OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808 Author: Jeanne Boyarsky, Scott Selikoff Length: 432 pages Edition: 1 Language: English Publisher: Sybex ...
在这个名为"欧姆龙OMRON PLC编程软件CX-Programmer 9.4和 9.5版本 CX-ONE编程软件.zip"的压缩包中,包含了用于编程欧姆龙PLC的CX-Programmer的两个重要版本:9.4和9.5。 CX-Programmer是一款功能丰富的软件,它支持...
开源软件通常遵循特定的许可证,比如MIT或Apache,确保了代码的自由使用和分享。 "A Programmer Prepares"软件可能包含以下功能: 1. **题目库**:包含各种编程题目,涵盖算法、数据结构、操作系统、网络、数据库...
全彩图,内容清晰,有详细书签便于查阅。是WPF方面的最新力作。
JavaSE8 Programmer Associate课程是为想要深入理解和掌握Java Standard Edition 8(Java SE 8)编程基础的人设计的。这个课程通常包括一系列的学习模块,旨在帮助学员通过Oracle Certified Associate, Java SE 8 ...
程序员考试刷题OCA_Java_Programmer_I_Capitolele: 第 1 章 Java 构建块 理解 Java 类结构:字段和方法注释类与文件 编写 main() 方法 了解包声明和导入:通配符冗余导入命名冲突 创建新包 考试中的代码格式 创建...
programmer_practices algorithm practices 最优解Book: 双栈实现getMin Stack. 双栈实现Queue. 递归和栈实现逆序操作一个栈. 仅用一个栈排序另一个栈. 打印两个有序链表的公共部分. 删除单双链表倒数第K个Node. ...
《SmartRF Flash Programmer V1.12.8:掌握低功耗射频MCU的闪存编程技术》 在物联网和无线通信领域,高效的设备编程工具是开发过程中的关键环节。其中,SmartRF Flash Programmer V1.12.8是由德州仪器(Texas ...
awesome-programmer-source 专注分享高质量程序员相关资源,包括优质文章、教程、GitHub(Gitee)仓库、公众号等优质资源 数据结构与算法 资源名称 资源分类 资源描述 刷题网站 刷题必备,学习算法没刷过LeetCode...