XML userd in Java -----A DOM EXAMPLE

转载 2006年05月19日 11:48:00

The class you need listed here:

>>>>> StuInfo.xsl   StuInfo.xml Student.dtd master.txt MyDOMParser.class

StuInfo.xsl

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:LIT="http://www.lit.edu.cn/student/"
        version="1.0">

    <xsl:template match="LIT:StuInfo">
        <html>
            <head>
                <title>Student Information</title>
            </head>
            <body>
                <xsl:apply-templates select="*"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="LIT:student">
        <li>Name:<xsl:value-of select="LIT:name"/></li>
        <li>Sex:<xsl:value-of select="LIT:sex"/></li>
        <xsl:for-each select="LIT:lesson">
            <li>Lesson:<xsl:value-of select="LIT:lessonName"/>(<xsl:value-of select="LIT:lessonScore"/>)</li>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="LIT:breakLine">
        <hr/>
    </xsl:template>

    <xsl:template match="master">
        <xsl:copy-of select="*"/>
    </xsl:template>

</xsl:stylesheet>

Student.dtd

<!ELEMENT LIT:StuInfo ((LIT:student, LIT:breakLine)*, LIT:master)>
<!ATTLIST LIT:StuInfo xmlns:LIT CDATA #REQUIRED>
<!ELEMENT LIT:student (LIT:name, LIT:sex, LIT:lesson*)>
<!ELEMENT LIT:name (#PCDATA)>
<!ELEMENT LIT:sex (#PCDATA)>
<!ELEMENT LIT:lesson (LIT:lessonName, LIT:lessonScore)>
<!ELEMENT LIT:lessonName (#PCDATA)>
<!ELEMENT LIT:lessonScore (#PCDATA)>
<!ELEMENT LIT:breakLine EMPTY>
<!ELEMENT LIT:master (#PCDATA)>
<!ENTITY masterName SYSTEM "master.txt">

StuInfo.xml

<?xml version="1.0"?>
<?xml-stylesheet href="xsl/StuInfo.xsl" type="text/xsl"?>
<!DOCTYPE LIT:StuInfo SYSTEM "student.dtd">

<LIT:StuInfo xmlns:LIT="http://www.baidu.com/">
   
    <LIT:student>
        <LIT:name>bigmouse</LIT:name>
        <LIT:sex>male</LIT:sex>
        <LIT:lesson>
            <LIT:lessonName>math </LIT:lessonName>
            <LIT:lessonScore>60</LIT:lessonScore>
        </LIT:lesson>
        <LIT:lesson>
            <LIT:lessonName>Englist</LIT:lessonName>
            <LIT:lessonScore>59</LIT:lessonScore>
        </LIT:lesson>
        <LIT:lesson>
            <LIT:lessonName>autoCAD</LIT:lessonName>
            <LIT:lessonScore>80</LIT:lessonScore>
        </LIT:lesson>
        <LIT:lesson>
            <LIT:lessonName>SCM</LIT:lessonName>
            <LIT:lessonScore>90</LIT:lessonScore>
        </LIT:lesson>
        <LIT:lesson>
            <LIT:lessonName>mechanics</LIT:lessonName>
            <LIT:lessonScore>61</LIT:lessonScore>
        </LIT:lesson>
    </LIT:student>

    <LIT:breakLine/>

    <LIT:student>
        <LIT:name>coco</LIT:name>
        <LIT:sex>female</LIT:sex>
        <LIT:lesson>
            <LIT:lessonName>Math</LIT:lessonName>
            <LIT:lessonScore>90</LIT:lessonScore>
        </LIT:lesson>
        <LIT:lesson>
            <LIT:lessonName>English</LIT:lessonName>
            <LIT:lessonScore>95</LIT:lessonScore>
        </LIT:lesson>
        <LIT:lesson>
            <LIT:lessonName>C++</LIT:lessonName>
            <LIT:lessonScore>80</LIT:lessonScore>
        </LIT:lesson>
        <LIT:lesson>
            <LIT:lessonName>Java</LIT:lessonName>
            <LIT:lessonScore>85</LIT:lessonScore>
        </LIT:lesson>
    </LIT:student>

    <LIT:breakLine/>

    <LIT:master>&masterName;</LIT:master>

</LIT:StuInfo>

MyDOMParser.class
package com.test.XMLTest;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import java.util.*;

public class MyDOMParser {
 //名字空间
 private String strNamespace = "http://www.baidu.com/";

 //一个学生的资料
 private Hashtable htbStudent = new Hashtable();

 //所有学生的向量列表
 private Vector vStuInfo = new Vector();

 public MyDOMParser() {
 }

 public static void main(String[] args) {
  if (args.length != 1) {
   System.out.println("Usage:java MyDOMParser [XML File URI]");
  }

  MyDOMParser myDOMParser = new MyDOMParser();
  myDOMParser.parseXMLFile("com/test/XMLTest/StuInfo.xml");
 }

 /**
  * 解析文档
  *
  * @param fileURI
  */
 public void parseXMLFile(String fileURI) {
  try {
   DocumentBuilderFactory factory = DocumentBuilderFactory
     .newInstance();
   //允许名字空间
   factory.setNamespaceAware(true);
   //允许验证
   factory.setValidating(true);
   //获得DocumentBuilder的一个实例
   DocumentBuilder builder = factory.newDocumentBuilder();
   //解析文档,并获得一个Document实例。
   Document doc = builder.parse(fileURI);
   //获得根节点StuInfo
   Element elmtStuInfo = doc.getDocumentElement();
   //得到所有student节点
   NodeList nlStudent = elmtStuInfo.getElementsByTagNameNS(
     strNamespace, "student");

   System.out.println("**** Student information start ****");

   //循环输出每一个学生资料
   for (int i = 0; i < nlStudent.getLength(); i++) {
    //当前student节点元素
    Element elmtStudent = (Element) nlStudent.item(i);

    NodeList nlCurrent = elmtStudent.getElementsByTagNameNS(
      strNamespace, "name");
    System.out.println("Name:"
      + nlCurrent.item(0).getFirstChild().getNodeValue());

    nlCurrent = elmtStudent.getElementsByTagNameNS(strNamespace,
      "sex");
    System.out.println("Sex:"
      + nlCurrent.item(0).getFirstChild().getNodeValue());

    nlCurrent = elmtStudent.getElementsByTagNameNS(strNamespace,
      "lesson");

    for (int j = 0; j < nlCurrent.getLength(); j++) {
     Element elmtLesson = (Element) nlCurrent.item(j);
     NodeList nlLesson = elmtLesson.getElementsByTagNameNS(
       strNamespace, "lessonName");
     System.out.print(nlLesson.item(0).getFirstChild()
       .getNodeValue());
     System.out.print(":");
     nlLesson = elmtLesson.getElementsByTagNameNS(strNamespace,
       "lessonScore");
     System.out.print(nlLesson.item(0).getFirstChild()
       .getNodeValue());
     System.out.print("/n");
    }

    System.out.println("------------------------------------");
   }

   System.out.println("**** Student information end ****");
  } catch (SAXException saxe) {
   System.out.println(saxe.getMessage());
  } catch (IOException ioe) {
   System.out.println(ioe.getMessage());
  } catch (ParserConfigurationException pce) {
   System.out.println(pce.getMessage());
  }
 }
}

Over .

Wish you a good luck

举报

相关文章推荐

Spring AOP + AspectJ in XML configuration example

For those don’t like annotation or using JDK 1.4, you can use AspectJ in XML based instead.Review la...

Spring AOP + AspectJ in XML configuration example

Review last CustomerBo interface again, with few methods, later you will learn how to intercept it v...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

A SOAP Example in Java

A SOAP Example in Java You can use SOAP with Java by using Web services, but doing so is more advan...

Asynchronous example Thrift in Java

Asynchronous example Thrift in Java 上一篇试了下同步的例子,这篇来玩玩异步调用是怎样滴~直接上代码~

Spring AOP Advices - Before advice example - xml based configuration - See more at: http://www.java2

In Spring, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modular...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)