junqing的个人博客分享 http://blog.sciencenet.cn/u/junqing

博文

XML 语言学习笔记

已有 4400 次阅读 2012-5-19 19:18 |系统分类:科研笔记| 学习

参考书籍:

 Java2 核心技术卷2:高级特性(第7版)英文版, 作者Cay S.Horstmann  Gary Cornell

人民邮电出版社

1) The structure of an XML document

<?xml version=”1.0” encoding=”UTF-8”?>

<!--  This is an instruction.  -->

<!DOCTYPE configuration ….>

<configuration>

   <title>

     <font>

        <name> Helvetica </name>

        <size unit=”pt”> 25 </size>

     </font>

</title>

</configuration>

2) Parsing an XML document

There are two kinds of XML parsers supported by the Java library, and I study about the Document Object Model (DOM) parser that reads an XML document into a tree structure.

The code reads an XML file using DOM parser. DOM parser loads the XML file into the memory and makes an object model of it. This object model can be traversed to get its elements.

This code will parse the MyXMLFile.xml file and print its elements to the console.

 

MyXMLFile.xml

 

<?xml version="1.0" encoding="gb2312" ?>

<company>

<employee>

    <firstname>Tom</firstname>

    <lastname>Cruise</lastname>

  </employee>

<employee>

     <firstname>Paul</firstname>

     <lastname>Enderson</lastname>

    </employee>

<employee>

     <firstname>George</firstname>

     <lastname>Bush</lastname>

    </employee>

  </company>

 

package DOMpaserXML;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class readXML {

    /**

     * @param args

     */

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       try {

             File file = new File("c:\MyXMLFile.xml");

             DocumentBuilderFactory  factory = DocumentBuilderFactory.newInstance();

             DocumentBuilder builder = factory.newDocumentBuilder();

             Document doc = builder.parse(file);

            

             doc.getDocumentElement().normalize();

             System.out.println("Root element " + doc.getDocumentElement().getNodeName());

             NodeList nodeLst = doc.getElementsByTagName("employee");

             System.out.println("Information of all employees");

 

             for (int i = 0; i < nodeLst.getLength(); i++) {

 

               Node fstNode = nodeLst.item(i);

              

               if (fstNode instanceof Element) {

              // if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

            

                 Element fstElmnt = (Element) fstNode;

                 NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");

                 Element fstNmElmnt = (Element) fstNmElmntLst.item(0);

                 NodeList fstNm = fstNmElmnt.getChildNodes();

                 System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());

                 NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");

                 Element lstNmElmnt = (Element) lstNmElmntLst.item(0);

                 NodeList lstNm = lstNmElmnt.getChildNodes();

                 System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());

               }

             }

             } catch (Exception e) {

               e.printStackTrace();

             }

    }

}

 

3) generating XML documents

 

 

4) XST transformation

 

XSTL的全称是可扩展的样式表转换语言(即Extensible Stylesheet Transformation Language)。它是一种用来转换XML文档结构的语言。为了使XML文档中存储的数据便于阅读理解,往往需要将信息显示出来或者打印出来,例如将存储的数据转换成一个HTML文件,一个PDF文件,甚至是一段声音。同样,为了使数据适合不同的应用程序,就必须提供能够将一种数据格式转换为另一种数据格式的方法,比如需求格式可能是一个文本文件,一个SQL语句,一个HTTP信息,一定顺序的数据调用等。而XSLT就是用来实现这种转换功能的语言。XSLT最主要的功能就是将XML转换为HTML

 

Xpath

XML是一个完整的树型结构的文档。在转换XML文档时往往需要处理其中的一部分(节点)数据,那么如何查找和定位XML文档中的信息呢?XPath就是一种专门用来在XML文档中查找信息的语言。XPath隶属XSTL,因此我们通常会将XSTL语法和XPath语法结合在一起说。

用一种比较好理解的解释:如果将XML文档看作一个数据库,XPath就是SQL查询语言;如果将XML文档看成DOS目录结构,XPath就是cd,dir等目录操作命令的集合。

 

 

 



https://wap.sciencenet.cn/blog-458387-572815.html

上一篇:汉诺塔算法的递归算法C++实现
收藏 IP: 202.120.38.*| 热度|

0

评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-19 12:26

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部