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
分享到:
相关推荐
Pragmatic Programmer, The: From Journeyman to Master
解决在epcs_flash_controller烧录时报出的“Error: Error code: 8 for command: nios2-flash-programmer”错误
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 ...
程序员考试刷题Perguntas 团结 这是我为练习而制作的应用程序。 它有两种模式,练习和考试。 一半的问题来自官方来源,另一半来自书中。 我已经使这个应用程序尽可能接近考试形式。 ...您可以选择进行模拟测试、特定...
Android-A Programmer's Guide(译)
Apache Thrift程序员指南以下示例中的源代码:Apache Thrift程序员指南 本书分为三个部分:第一部分-Apache Thrift概述对Apache Thrift及其体系结构的高级介绍。 这部分的例子非常有趣。 本部分还介绍了基本的Apache...
M-x package-install RET programmer-dvorak 然后将其添加到您的 emacs 配置文件中。 (require 'programmer-dvorak) 用法 `programmer-dvorak' 将被注册为 Emacs 中的标准输入法,选择它: M-x set-input-...
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 ...
程序员的刷题软件 A Programmer Prepares Being a programmer... 联系方式 email: github:
全彩图,内容清晰,有详细书签便于查阅。是WPF方面的最新力作。
JavaSE8_Programmer_Associate 课程路径链接
程序员考试刷题OCA_Java_Programmer_I_Capitolele: 第 1 章 Java 构建块 理解 Java 类结构:字段和方法注释类与文件 编写 main() 方法 了解包声明和导入:通配符冗余导入命名冲突 创建新包 考试中的代码格式 创建...
programmer_practices algorithm practices 最优解Book: 双栈实现getMin Stack. 双栈实现Queue. 递归和栈实现逆序操作一个栈. 仅用一个栈排序另一个栈. 打印两个有序链表的公共部分. 删除单双链表倒数第K个Node. ...
日常程序员挑战 这些是发布到每日程序员挑战赛 subreddit 的一系列挑战! 当我没有动力完成任何工作时,这些都是很好的选择。 它们可以非常有效地让您的大脑摆脱恐惧!
awesome-programmer-source 专注分享高质量程序员相关资源,包括优质文章、教程、GitHub(Gitee)仓库、公众号等优质资源 数据结构与算法 资源名称 资源分类 资源描述 刷题网站 刷题必备,学习算法没刷过LeetCode...
咖啡因博客什么是咖啡因博客? 咖啡因是一个 [鹈鹕] 主题,基于 [Bootstrap] 和午夜的大量咖啡。 是鹈鹕主题改编/基于StartBootstrap:干净的博客
cmd-programmer-org hpsdr p1协议的命令行程序员是kv0s回购的一部分。
USB-Pro-PIC-Programmer-v2 OshonSoft 兼容 USB PIC 编程器 背景 USB Pro PIC-PIC18-PIC16-AVR 编程器是由 Vladimir Soso 创建的,他是PIC 模拟器 IDE 的。 该项目是带有附加 DC-DC 转换器的 USB Pro PIC 编程器的...