🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.xml
Article #7342

ϣ��ʹ��JDOM�����Ѳ��ߴͽ�

#7342
From: ��������
Date: Sat, 09 Aug 2003 06:55
124 lines
4588 bytes
Ҫ���������������,test.xml�����,test.xsl�Ǹ�test.xml�õ���ʽ��,��ͨ����
�ַ���ʵ��transform,һ������sun�ṩ��jdk���е�����,һ������jdom�ṩ������
��,���������,ʹ��jdk�������Ļ�,��ʽ���� ����ȷ��תΪ ����jdom
���Ļ�,��ʽ���е� ������ȷ��תΪ ,���������һ���޷���ʾ���ַ�
��������ʹ�õ�ʱ���Ƿ�Ҳ�������������.�������ļ��Ĵ���:
=========test.xml============
<?xml version="1.0" encoding="GB2312"?>
<portfolio>
  <stock>
    <symbol>SUNW</symbol>
    <name>Sun Microsystems</name>
    <price>17.1</price>
  </stock>
  <stock>
    <symbol>AOL</symbol>
    <name>America Online</name>
    <price>51.05</price>
  </stock>
  <stock>
    <symbol>IBM</symbol>
    <name>International Business 
    Machines</name>
    <price>116.10</price>
  </stock>
  <stock>
    <symbol>MOT</symbol>
    <name>MOTOROLA</name>
    <price>15.20</price>
  </stock>
</portfolio>
===========test.xsl==========
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version=
"1.0">

<xsl:output method="html" indent="yes" encoding="GB2312"/>

<xsl:template match="/portfolio">
  <html>

  <body>
   <xsl:for-each select="stock">
     &#160;<xsl:value-of select="name" />
   </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
==============Xml2html.java===================
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.*;

public class Xml2html {
    public static void main(String[] args)
        throws
            TransformerException,
            TransformerConfigurationException,
            IOException {
        Transformer transformer =
        TransformerFactory.newInstance().newTransformer(new StreamSource(
args[1]));
        transformer.transform(
            new StreamSource(args[0]),
            new StreamResult(System.out));

    }
}
����:
<html>
<body>
     &nbsp;Sun Microsystems
     &nbsp;America Online
     &nbsp;International Business 
    Machines
     &nbsp;MOTOROLA</body>
</html>
=================Jdom2html==================
import java.util.Vector;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;

public class Jdom2html {
    public static void main(String[] args) throws Exception {

        // Use JAXP to get a transformer
        Transformer transformer =
            TransformerFactory.newInstance().newTransformer(
                new StreamSource(args[1]));

                
        JDOMResult result = new JDOMResult();
//        Run the transformation
        transformer.transform(new JDOMSource(new SAXBuilder().build(args[
0])), result);
        Document doc = result.getDocument();

        //Display the results
        XMLOutputter outp = new XMLOutputter("  ", true, "gb2312");
        outp.output(doc, System.out);
    }
}
����:
<?xml version="1.0" encoding="gb2312"?>
<html>
  <body>
     ?Sun Microsystems
     ?America Online
     ?International Business 
    Machines
     ?MOTOROLA</body>
</html>

--

�� ��Դ:��BBS ˮľ�廪վ smth.org��[FROM: 162.105.58.149]
Technical Details
Message-ID: <000000$Byr@smth.org>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!elnk-nf2-pas!newsfeed.earthlink.net!sjc70.webusenet.com!news.webusenet.com!maily.cic.tsinghua.edu.cn!SMTH

Show Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB
X-Google-Thread: 115571,4bf69aa0e04e07bd,start
X-Google-Attributes: gid115571,public
X-Google-ArrivalTime: 2003-08-09 00:00:08 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!elnk-nf2-pas!newsfeed.earthlink.net!sjc70.webusenet.com!news.webusenet.com!maily.cic.tsinghua.edu.cn!SMTH
From: iamangry@smth.org (��������)
Newsgroups: cn.bbs.comp.xml
Subject: ϣ��ʹ��JDOM�����Ѳ��ߴͽ�
Date: 09 Aug 2003 06:55:33 GMT
Organization: BBS ˮľ�廪վ
Message-ID: <000000$Byr@smth.org>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: XML/I/M.1060412084.U0
Lines: 124