设为首页 收藏本站
查看: 549|回复: 0

[经验分享] Crazy Programmer Homework One: Apache Xalan Studying

[复制链接]

尚未签到

发表于 2017-1-7 09:18:34 | 显示全部楼层 |阅读模式
  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:
  
DSC0000.png
 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-324932-1-1.html 上篇帖子: apache日志 LogFormat参数说明 [转] 下篇帖子: BeanUtils--org.apache.commons.beanutils.BeanUtils
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表