`
kylinsoong
  • 浏览: 236939 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Crazy Programmer Homework One: Apache Xalan Studying

    博客分类:
  • XML
阅读更多

      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
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics