新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> XML网站展示,XML源代码,XML编程示例。 本版仅接受原创、转贴、网站展示,具体的技术交流请前往各相关版块。
    [返回] 计算机科学论坛XML.ORG.CN讨论区 - XML技术『 XML源码及示例(仅原创和转载) 』 → 那个又用java读取xml的一个最基本的例子啊 ! 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 14810 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 那个又用java读取xml的一个最基本的例子啊 ! 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     xhxasdf 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:20
      积分:133
      门派:XML.ORG.CN
      注册:2005/1/4

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xhxasdf发送一个短消息 把xhxasdf加入好友 查看xhxasdf的个人资料 搜索xhxasdf在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xhxasdf的博客楼主
    发贴心情 那个又用java读取xml的一个最基本的例子啊 !

    谢谢指教!
    直接回复可以
    发到我的邮箱也可以!

    xhxaimm@yahoo.com.cn


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/4 16:29:00
     
     雪芙蓉 美女呀,离线,快来找我吧!
      
      
      等级:大一新生
      文章:1
      积分:66
      门派:XML.ORG.CN
      注册:2005/5/9

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给雪芙蓉发送一个短消息 把雪芙蓉加入好友 查看雪芙蓉的个人资料 搜索雪芙蓉在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看雪芙蓉的博客2
    发贴心情 
    我也要啊,一直看不懂是如何进行读取的,我的QQ:116587222。E-MAIL:luxinfeng2005@126.com,
    多谢
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/9 21:44:00
     
     anhy 美女呀,离线,快来找我吧!
      
      
      等级:大一新生
      文章:2
      积分:69
      门派:XML.ORG.CN
      注册:2005/3/15

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给anhy发送一个短消息 把anhy加入好友 查看anhy的个人资料 搜索anhy在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看anhy的博客3
    发贴心情 
    下面是一个例子,可以将运行参数中指定的XML文件输出在输出框架:
    import javax.xml.parsers.*;
    import org.w3c.dom.*;

    public class DOM_sample
    {
        static String displayText[] = new String[1000];
        static int numberLines = 0;

        public static void main(String args[])
        {
            try {
                DocumentBuilderFactory factory =
                    DocumentBuilderFactory.newInstance();

                DocumentBuilder builder = null;
                try {
                    builder = factory.newDocumentBuilder();
                }
                catch (ParserConfigurationException e) {}

                Document document = null;
                document = builder.parse(args[0]);

                childLoop(document, "");

            } catch (Exception e) {
                e.printStackTrace(System.err);
            }

            for(int loopIndex = 0; loopIndex < numberLines; loopIndex++){
                System.out.println(displayText[loopIndex]) ;
            }
        }

        public static void childLoop(Node node, String indentation)
        {
            if (node == null) {
                return;
            }

            int type = node.getNodeType();

            switch (type) {
                case Node.DOCUMENT_NODE: {
                    displayText[numberLines] = indentation;
                    displayText[numberLines] +=
                        "<?xml version=\"1.0\" encoding=\""+
                      "UTF-8" + "\"?>";
                    numberLines++;
                    childLoop(((Document)node).getDocumentElement(), "");
                    break;
                 }

                 case Node.ELEMENT_NODE: {
                     displayText[numberLines] = indentation;
                     displayText[numberLines] += "<";
                     displayText[numberLines] += node.getNodeName();

                     int length = (node.getAttributes() != null) ?
                         node.getAttributes().getLength() : 0;
                     Attr attributes[] = new Attr[length];
                     for (int loopIndex = 0; loopIndex < length; loopIndex++) {
                         attributes[loopIndex] =
                             (Attr)node.getAttributes().item(loopIndex);
                     }

                     for (int loopIndex = 0; loopIndex < attributes.length;
                         loopIndex++) {
                         Attr attribute = attributes[loopIndex];
                         displayText[numberLines] += " ";
                         displayText[numberLines] += attribute.getNodeName();
                         displayText[numberLines] += "=\"";
                         displayText[numberLines] += attribute.getNodeValue();
                         displayText[numberLines] += "\"";
                     }
                     displayText[numberLines] += ">";

                     numberLines++;

                     NodeList childNodes = node.getChildNodes();
                     if (childNodes != null) {
                         length = childNodes.getLength();
                         indentation += "    ";
                         for (int loopIndex = 0; loopIndex < length; loopIndex++ ) {
                            childLoop(childNodes.item(loopIndex), indentation);
                         }
                     }
                     break;
                 }

                 case Node.TEXT_NODE: {
                     displayText[numberLines] = indentation;
                     String trimmedText = node.getNodeValue().trim();
                     if(trimmedText.indexOf("\n") < 0 && trimmedText.length() > 0){
                         displayText[numberLines] += trimmedText;
                         numberLines++;
                     }
                     break;
                 }

                 case Node.PROCESSING_INSTRUCTION_NODE: {
                     displayText[numberLines] = indentation;
                     displayText[numberLines] += "<?";
                     displayText[numberLines] += node.getNodeName();
                     String text = node.getNodeValue();
                     if (text != null && text.length() > 0) {
                         displayText[numberLines] += text;
                     }
                     displayText[numberLines] += "?>";
                     numberLines++;
                     break;
                 }

                 case Node.CDATA_SECTION_NODE: {
                     displayText[numberLines] = indentation;
                     displayText[numberLines] += "<![CDATA[";
                     displayText[numberLines] += node.getNodeValue();
                     displayText[numberLines] += "]]>";
                     numberLines++;
                     break;
                }
            }

            if (type == Node.ELEMENT_NODE) {
                displayText[numberLines] = indentation.substring(0,
                    indentation.length() - 4);
                displayText[numberLines] += "</";
                displayText[numberLines] += node.getNodeName();
                displayText[numberLines] += ">";
                numberLines++;
                indentation += "    ";
            }
        }
    }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/10 8:28:00
     
     xhxasdf 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:20
      积分:133
      门派:XML.ORG.CN
      注册:2005/1/4

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xhxasdf发送一个短消息 把xhxasdf加入好友 查看xhxasdf的个人资料 搜索xhxasdf在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xhxasdf的博客4
    发贴心情 
    谢谢楼上的mm
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/11 16:57:00
     
     rosering 美女呀,离线,快来找我吧!
      
      
      等级:大一新生
      文章:8
      积分:95
      门派:XML.ORG.CN
      注册:2005/5/10

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给rosering发送一个短消息 把rosering加入好友 查看rosering的个人资料 搜索rosering在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看rosering的博客5
    发贴心情 
    有没有用JAVA编写的在网络中搜索XML的程序??
    谢谢大家了!!!!!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/13 13:03:00
     
     不知道为什么 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:71
      门派:XML.ORG.CN
      注册:2005/5/14

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给不知道为什么发送一个短消息 把不知道为什么加入好友 查看不知道为什么的个人资料 搜索不知道为什么在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看不知道为什么的博客6
    发贴心情 
    用JDOM很容易的,例子Google一下可以找到一堆
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/15 10:48:00
     
     fengliangjun 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:1
      积分:59
      门派:XML.ORG.CN
      注册:2005/5/16

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给fengliangjun发送一个短消息 把fengliangjun加入好友 查看fengliangjun的个人资料 搜索fengliangjun在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看fengliangjun的博客7
    发贴心情 
    XML 文档(catalog.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <catalog>
    <!--An XML Catalog-->
    <?target instruction?>
      <journal title="XML Zone"
                      publisher="IBM developerWorks">

    <article level="Intermediate" date="December-2001">
    <title>Java configuration with XML Schema</title>
    <author>
         <firstname>Marcello</firstname>
         <lastname>Vitaletti</lastname>
    </author>
      </article>
      </journal>
    </catalog>

    生成 上述  catalog.xml 的程序(XmlDom4J.java)

    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.XMLWriter;
    import java.io.*;

    public class XmlDom4J{


    public void generateDocument(){
    Document document = DocumentHelper.createDocument();
         Element catalogElement = document.addElement("catalog");
         catalogElement.addComment("An XML Catalog");
         catalogElement.addProcessingInstruction("target","text");
         Element journalElement =  catalogElement.addElement("journal");
         journalElement.addAttribute("title", "XML Zone");
         journalElement.addAttribute("publisher", "IBM developerWorks");


         Element articleElement=journalElement.addElement("article");
         articleElement.addAttribute("level", "Intermediate");
         articleElement.addAttribute("date", "December-2001");
         Element  titleElement=articleElement.addElement("title");
         titleElement.setText("Java configuration with XML Schema");
         Element authorElement=articleElement.addElement("author");
         Element  firstNameElement=authorElement.addElement("firstname");
         firstNameElement.setText("Marcello");
         Element lastNameElement=authorElement.addElement("lastname");
         lastNameElement.setText("Vitaletti");

         document.addDocType("catalog",
                               null,"file://c:/Dtds/catalog.dtd");
        try{
        XMLWriter output = new XMLWriter(
                new FileWriter( new File("c:/catalog/catalog.xml") ));
            output.write( document );
            output.close();
            }
         catch(IOException e){System.out.println(e.getMessage());}
    }

    public static void main(String[] argv){
    XmlDom4J dom4j=new XmlDom4J();
    dom4j.generateDocument();
    }}

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/16 16:11:00
     
     rosering 美女呀,离线,快来找我吧!
      
      
      等级:大一新生
      文章:8
      积分:95
      门派:XML.ORG.CN
      注册:2005/5/10

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给rosering发送一个短消息 把rosering加入好友 查看rosering的个人资料 搜索rosering在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看rosering的博客8
    发贴心情 
    谢谢大家!!!!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/22 18:49:00
     
     Starplain 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:10
      积分:95
      门派:XML.ORG.CN
      注册:2005/4/9

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Starplain发送一个短消息 把Starplain加入好友 查看Starplain的个人资料 搜索Starplain在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看Starplain的博客9
    发贴心情 
    好东西,谢谢分享!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/23 7:04:00
     
     benben1234 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:8
      积分:72
      门派:XML.ORG.CN
      注册:2005/7/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给benben1234发送一个短消息 把benben1234加入好友 查看benben1234的个人资料 搜索benben1234在『 XML源码及示例(仅原创和转载) 』的所有贴子 引用回复这个贴子 回复这个贴子 查看benben1234的博客10
    发贴心情 
    好东西
    收藏!!!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/7/15 17:06:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 XML源码及示例(仅原创和转载) 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/7/25 9:48:30

    本主题贴数13,分页: [1] [2]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    1,015.625ms