以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 XML源码及示例(仅原创和转载) 』  (http://bbs.xml.org.cn/list.asp?boardid=32)
----  那个又用java读取xml的一个最基本的例子啊 !  (http://bbs.xml.org.cn/dispbbs.asp?boardid=32&rootid=&id=17876)


--  作者:xhxasdf
--  发布时间:5/4/2005 4:29:00 PM

--  那个又用java读取xml的一个最基本的例子啊 !
谢谢指教!
直接回复可以
发到我的邮箱也可以!

xhxaimm@yahoo.com.cn


--  作者:雪芙蓉
--  发布时间:5/9/2005 9:44:00 PM

--  
我也要啊,一直看不懂是如何进行读取的,我的QQ:116587222。E-MAIL:luxinfeng2005@126.com,
多谢
--  作者:anhy
--  发布时间:5/10/2005 8:28:00 AM

--  
下面是一个例子,可以将运行参数中指定的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 += "    ";
        }
    }
}


--  作者:xhxasdf
--  发布时间:5/11/2005 4:57:00 PM

--  
谢谢楼上的mm
--  作者:rosering
--  发布时间:5/13/2005 1:03:00 PM

--  
有没有用JAVA编写的在网络中搜索XML的程序??
谢谢大家了!!!!!
--  作者:不知道为什么
--  发布时间:5/15/2005 10:48:00 AM

--  
用JDOM很容易的,例子Google一下可以找到一堆
--  作者:fengliangjun
--  发布时间:5/16/2005 4:11:00 PM

--  
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();
}}



--  作者:rosering
--  发布时间:5/22/2005 6:49:00 PM

--  
谢谢大家!!!!
--  作者:Starplain
--  发布时间:5/23/2005 7:04:00 AM

--  
好东西,谢谢分享!
--  作者:benben1234
--  发布时间:7/15/2005 5:06:00 PM

--  
好东西
收藏!!!
--  作者:zy122
--  发布时间:8/4/2005 12:03:00 PM

--  
真是好东东.
--  作者:zwangus
--  发布时间:8/6/2005 10:56:00 AM

--  
感谢楼上mm
--  作者:tousei811123
--  发布时间:8/19/2005 3:03:00 PM

--  
谢谢楼主!
想请教个问题:
解析时,xml标签可以是全角的英数字吗?
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
4,734.375ms