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

DTD文件:SwordTypeDefinition.dtd

XML文件:SwordLib.xml

SwordLibrary SYSTEM "SwordTypeDefinition.dtd">

欢欣之刃

1000

10

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

{

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*/

}

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐