jaxp解析xml

前端之家收集整理的这篇文章主要介绍了jaxp解析xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <strong>jaxp是java提供的对xml的解析技术
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?><span style="color:#FF0000;">//单标签,HTML是双标签</span>
  4. <school>
  5.  
  6. <class id="c001">
  7.  
  8. <teacher>jack</teacher>
  9.  
  10. <student id="s001" birth="1990-2-1" sex="男" name="tom" />
  11.  
  12. <student id="s002" birth="1990-1-1" sex="男" name="Mary" />
  13.  
  14. <student id="s003" birth="1990-2-10" sex="男" name="M" />
  15. </class>
  16.  
  17. </school></strong>
  18.  
  19.  
  20. <pre name="code" class="java"><strong>public static void main(String[] args) throws Exception {
  21. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  22. DocumentBuilder builder = factory.newDocumentBuilder();
  23. Document doc = builder.parse(new File("r.xml"));<span style="color:#FF0000;">//默认就是当前项目的根文件</span>
  24. loop(doc);
  25. getElement(doc);
  26. }
  27.  
  28. private static void getElement(Document doc) {
  29. NodeList list = doc.getElementsByTagName("teacher");
  30.  
  31. for(int i = 0; i < list.getLength(); i++)
  32. {
  33. Node node = list.item(i);
  34. System.out.println("元素节点名 :"+node.getNodeName());
  35. System.out.println("元素节点内容 :"+node.getTextContent());
  36. //获取该元素上的属性
  37. NamedNodeMap n = node.getAttributes();
  38. for(int j = 0; j < n.getLength(); j++)
  39. {
  40. System.out.println("属性节点名 :"+n.item(j).getNodeName()+" 属性节点值 :"+n.item(j).getNodeValue());
  41. }
  42. System.out.println("---------------------------------------");
  43. }
  44. }
  45. </strong><pre name="code" class="html"><pre name="code" class="java"><strong><span style="color:#999900;">/*
  46. </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>
  1.  
元素节点名 :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 属性节点值 :男
---------------------------------------
  1.  
  1.  
  1.  
  1.  
  1.  
  1.  




  1. <pre name="code" class="java"><strong>当NodeList list = doc.getElementsByTagName("teacher");输出<span style="color:#999900;">
  2. </span></strong>
  1.  
元素节点名 :teacher
元素节点内容 :jack
---------------------------------------
  1.  
  1.  
  1. <pre name="code" class="java"><strong></strong>
  2. <pre name="code" class="html"><pre name="code" class="java"><strong><span style="color:#999900;">
  3. */</span></strong>
  1.  
  1.  
  1.  


//运用递归的方式获取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);}}
  1. <pre name="code" class="java"><strong><span style="color:#999900;">/* 输出
  2. school
  3. class
  4. teacher
  5. #text
  6. 值是jack
  7. student
  8. name is tom
  9. ID is s001
  10. birth is 1990-2-1
  11. sex is 男
  12. --------------------------------------
  13. student
  14. name is Mary
  15. ID is s002
  16. birth is 1990-1-1
  17. sex is 男
  18. --------------------------------------
  19. student
  20. name is M
  21. ID is s003
  22. birth is 1990-2-10
  23. sex is 男
  24. --------------------------------------
  25. */</span></strong>
  1.  
  1.  
  1.  
通过两种不同的遍历可以发现,xml是通过dom树的结构组织的,类似于HTML的dom树组织结构。在Dom中 一切都是节点,无论是element text attribute都是节点,这些节点统一实现了Node这个接口。也可以在fFirefox中看Dom更直观。

猜你在找的XML相关文章