标签:2-2 enc 视频 system 学习 x64 ast java 相对路径
礼悟:
好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。
jdk:1.8
xml:1.0
os:windows7 x64
ide:MyEclipse 2017
xml文件
<?xml version="1.0" encoding="UTF-8"?> <shopping> <book> <name id="0001">灵枢</name> <price discount="90" originalPrice="100">90</price> <count>3</count> <comment>默认好评</comment> </book> <book> <name id="0002">素问</name> <price discount="90" originalPrice="200">180</price> <count>5</count> <comment>默认好评</comment> </book> <book> <name id="0003">伤寒杂病论</name> <price discount="90" originalPrice="300">270</price> <count>7</count> <comment>默认好评</comment> </book> </shopping>
代码
package jizuiku.xml_E;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* jaxp getElementsByTagName 得到指定名字的所有标签的文本内容
*
* @author 给最苦
* @version V17.10.10
*/
public class JaxpDemo {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// 创建解析器工厂
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
// 创建解析器
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// 解析xml返回document
// uri为相对路径
String uri = "src\\jizuiku\\xml_E\\shopping.xml";
// Document -> import org.w3c.dom.Document;
Document document = builder.parse(uri);
// 得到所有的name元素
NodeList nameNodeList = document.getElementsByTagName("name");
for (int i = 0; i < nameNodeList.getLength(); i++) {
System.out.println(nameNodeList.item(i).getTextContent());
}
}
}
执行结果
xml优秀,值得学习。
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
jaxp dom-getElementsByTagName 得到指定名字的所有标签的文本内容
标签:2-2 enc 视频 system 学习 x64 ast java 相对路径
原文地址:http://www.cnblogs.com/jizuiku/p/7680711.html