- <strong>jaxp是java提供的对xml的解析技术
- <?xml version="1.0" encoding="UTF-8"?><span style="color:#FF0000;">//单标签,HTML是双标签</span>
- <school>
- <class id="c001">
- <teacher>jack</teacher>
- <student id="s001" birth="1990-2-1" sex="男" name="tom" />
- <student id="s002" birth="1990-1-1" sex="男" name="Mary" />
- <student id="s003" birth="1990-2-10" sex="男" name="M" />
- </class>
- </school></strong>
- <pre name="code" class="java"><strong>public static void main(String[] args) throws Exception {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(new File("r.xml"));<span style="color:#FF0000;">//默认就是当前项目的根文件夹</span>
- loop(doc);
- getElement(doc);
- }
- private static void getElement(Document doc) {
- NodeList list = doc.getElementsByTagName("teacher");
- for(int i = 0; i < list.getLength(); i++)
- {
- Node node = list.item(i);
- System.out.println("元素节点名 :"+node.getNodeName());
- System.out.println("元素节点内容 :"+node.getTextContent());
- //获取该元素上的属性
- NamedNodeMap n = node.getAttributes();
- for(int j = 0; j < n.getLength(); j++)
- {
- System.out.println("属性节点名 :"+n.item(j).getNodeName()+" 属性节点值 :"+n.item(j).getNodeValue());
- }
- System.out.println("---------------------------------------");
- }
- }
- </strong><pre name="code" class="html"><pre name="code" class="java"><strong><span style="color:#999900;">/*
- </span></strong><pre name="code" class="html"><pre name="code" class="java"><strong></strong><pre name="code" class="html"><pre name="code" class="java"><pre name="code" class="html"><pre name="code" class="java"><strong><span style="color:#999900;">/* </span></strong><pre name="code" class="html"><pre name="code" class="java"><strong>当NodeList list = doc.getElementsByTagName("student");输出</strong>
元素节点名 :student
元素节点内容 :
属性节点名 :birth 属性节点值 :1990-2-1
属性节点名 :id 属性节点值 :s001
属性节点名 :name 属性节点值 :tom
属性节点名 :sex 属性节点值 :男
---------------------------------------
元素节点名 :student
元素节点内容 :
属性节点名 :birth 属性节点值 :1990-1-1
属性节点名 :id 属性节点值 :s002
属性节点名 :name 属性节点值 :Mary
属性节点名 :sex 属性节点值 :男
---------------------------------------
元素节点名 :student
元素节点内容 :
属性节点名 :birth 属性节点值 :1990-2-10
属性节点名 :id 属性节点值 :s003
属性节点名 :name 属性节点值 :M
属性节点名 :sex 属性节点值 :男
---------------------------------------
- <pre name="code" class="java"><strong>当NodeList list = doc.getElementsByTagName("teacher");输出<span style="color:#999900;">
- </span></strong>
元素节点名 :teacher
元素节点内容 :jack
---------------------------------------
- <pre name="code" class="java"><strong></strong>
- <pre name="code" class="html"><pre name="code" class="java"><strong><span style="color:#999900;">
- */</span></strong>
//运用递归的方式获取xml文件所有的节点private static void loop(Node n) {NodeList list = n.getChildNodes(); for (int i = 0; i < list.getLength(); i++){Node nn = list.item(i);System.out.println(nn.getNodeName());
if(nn.getNodeValue()!=null){System.out.print("值是"+nn.getNodeValue());System.out.println();}if(nn.getNodeName()=="student"){Element s = (Element) nn; //node是Node接口的一个实现类ElementSystem.out.println("name is "+s.getAttribute("name"));System.out.println("ID is "+s.getAttribute("id"));System.out.println("birth is "+s.getAttribute("birth"));System.out.println("sex is "+s.getAttribute("sex"));System.out.println("--------------------------------------");}loop(nn);}}
- <pre name="code" class="java"><strong><span style="color:#999900;">/* 输出
- school
- class
- teacher
- #text
- 值是jack
- student
- name is tom
- ID is s001
- birth is 1990-2-1
- sex is 男
- --------------------------------------
- student
- name is Mary
- ID is s002
- birth is 1990-1-1
- sex is 男
- --------------------------------------
- student
- name is M
- ID is s003
- birth is 1990-2-10
- sex is 男
- --------------------------------------
- */</span></strong>
通过两种不同的遍历可以发现,xml是通过dom树的结构组织的,类似于HTML的dom树组织结构。在Dom中 一切都是节点,无论是element text attribute都是节点,这些节点统一实现了Node这个接口。也可以在fFirefox中看Dom更直观。