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

来源:互联网 发布:英语口音测试软件 编辑:程序博客网 时间:2024/05/22 17:50

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

原创粉丝点击