码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习之Xml系列一:初始步骤、查询元素及属性、遍历元素

时间:2014-07-23 00:32:07      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:java   使用   文件   io   for   re   

话不多说,全在代码和注释说明里了。

DTD文件:SwordTypeDefinition.dtd

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT SwordLibrary (Sword*)>
<!ELEMENT Sword (SwordName,Price,Attack)>
<!ELEMENT SwordName (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Attack (#PCDATA)>
<!ATTLIST Sword sno CDATA #REQUIRED>
<!ATTLIST Price type CDATA #IMPLIED>
<!ATTLIST Attack factor CDATA "1.0">

XML文件:SwordLib.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE SwordLibrary SYSTEM "SwordTypeDefinition.dtd">
<SwordLibrary>
<Sword sno=‘s1‘>
<SwordName>欢欣之刃</SwordName>
<Price>1000</Price>
<Attack>10</Attack>
</Sword>
</SwordLibrary>

java代码:

package JavaLeaner.XmlTest;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlDemo1 {

    /*
     * xml的helloworld
     * 初始三大步骤:
     * 1.建文档构造工厂
     * 2.建文档构造器
     * 3.建文档对象
     */
    @Test
    public void Test1() throws ParserConfigurationException, SAXException, IOException
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docDuilder = factory.newDocumentBuilder();
        Document doc = docDuilder.parse("src/JavaLeaner/XmlTest/SwordLib.xml");
        //注意:在eclipse中写dtd和xml,如果dtd本身不正确会导致xml编辑器无法提示出所引用dtd的元素和属性。
        //注意:只要xml和dtd文件格式正确,以上建立Document的操作无编译错误,并且运行也正常。
        //这说明建立Docment的操作与否仅仅与xml和dtd文件格式是否正确有关,而与xml是否符合dtd无关。(实验了子元素顺序跌倒,确实requried属性等接等够正常运行)
        //但是,xml不符合dtd,eclipse IDE会进行提示。
    }
    
    /*
     * xml查询特定元素
     * 
     */
    @Test
    public void Test2() throws ParserConfigurationException, SAXException, IOException
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docDuilder = factory.newDocumentBuilder();
        Document doc = docDuilder.parse("src/JavaLeaner/XmlTest/SwordLib.xml");

        NodeList list = doc.getElementsByTagName("Sword");
        Node node = list.item(0);
        Element swordTag=(Element)node;
        String snoText=swordTag.getAttribute("sno");
        String tagText=swordTag.getTextContent();

        System.out.println("sno:"+snoText);
        System.out.println("tagText:"+tagText);    
        //即使运行到这里,如果xml的sword元素没有sno属性,依然可以正常运行,只不过结果为空串
        //此时运行结果:
        //sno:
        //tagText:欢欣之刃100010
        //
        //xml正确时的结果:
        //sno:s1
        //tagText:欢欣之刃100010
        //注意:元素内有子元素,其getTextContent()得到的是取出xml格式后的纯内容
        
        
    }
    
    
    /*
     * xml遍历某一元素下的所有子元素和属性
     */
    @Test
    public void Test3() throws ParserConfigurationException, SAXException, IOException
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docDuilder = factory.newDocumentBuilder();
        Document doc = docDuilder.parse("src/JavaLeaner/XmlTest/SwordLib.xml");
        //注意:在eclipse中写dtd和xml,如果dtd本身不正确会导致xml编辑器无法提示出所引用dtd的元素和属性。
        //注意:只要xml和dtd文件格式正确,以上建立Document的操作无编译错误,并且运行也正常。
        //这说明建立Docment的操作与否仅仅与xml和dtd文件格式是否正确有关,而与xml是否符合dtd无关。(实验了子元素顺序跌倒,确实requried属性等接等够正常运行)
        //但是,xml不符合dtd,eclipse IDE会进行提示。
        
        NodeList list = doc.getElementsByTagName("Sword");
        //注意:NodeList是个接口,并且它没有实现Iterable接口,所以不能使用增强for循环来遍历
        //Can only iterate over an array or an instance of java.lang.Iterable
        //错误用法:for(Node node:list){}
        for(int i=0;i<list.getLength();i++)
        {
            Element swordTag=(Element)list.item(i);
            
            String snoText=swordTag.getAttribute("sno");
            System.out.println("sno:"+snoText);
            
            String SwordName = swordTag.getElementsByTagName("SwordName").item(0).getTextContent();
            String Price = swordTag.getElementsByTagName("Price").item(0).getTextContent();
            String Attack = swordTag.getElementsByTagName("Attack").item(0).getTextContent();
            System.out.println("SwordName:"+SwordName);
            System.out.println("Price:"+Price);
            System.out.println("Attack"+Attack);
            
/*            sno:s1
            SwordName:欢欣之刃
            Price:1000
            Attack10*/

            
        }
        
    }
    
    
}


Java学习之Xml系列一:初始步骤、查询元素及属性、遍历元素,布布扣,bubuko.com

Java学习之Xml系列一:初始步骤、查询元素及属性、遍历元素

标签:java   使用   文件   io   for   re   

原文地址:http://my.oschina.net/u/1156339/blog/293918

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!