---
title: "DOM解析"
author: "mhbjava"
date: "2006-03-22T10:59:00Z"
tags: []
slug: "dom"
draft: false
lastmod: "2006-03-22T10:59:00Z"
id: 26603
url: "https://mhbjava.iteye.com/blog/26603"
---

opera浏览器中文官方网站:http://download.pchome.net/internet/browser/browser/2645.html  
wap网:http://www.wapzj.com

2006.3.21  
解析xml

test.xml:  
&lt;?xml version="1.0" encoding="utf-8" ?&gt;  
&lt;U-MAX&gt;  
 &lt;PreSubscriptionNotify&gt;  
  &lt;MDN&gt;13312345678&lt;/MDN&gt;  
  &lt;SPCode&gt;12345&lt;/SPCode&gt;  
  &lt;ProductCode&gt;1234567890&lt;/ProductCode&gt;  
  &lt;TransactionID&gt;12345000A3E63B5E559E46F0ADA6E46B8DE2E1D2&lt;/TransactionID&gt;  
  &lt;EffectiveDate&gt;2003-07-07 10:00:00&lt;/EffectiveDate&gt;  
 &lt;/PreSubscriptionNotify&gt;  
&lt;/U-MAX&gt;

test.xsl:

&lt;?xml version="1.0" encoding="utf-8" ?&gt;  
&lt;xs:schema xmlns:xs="[http://www.w3.org/2001/XMLSchema](http://www.w3.org/2001/XMLSchema)"&gt;  
 &lt;xs:element name="U-MAX"&gt;  
  &lt;xs:complexType&gt;  
   &lt;xs:sequence&gt;  
    &lt;xs:element ref="PreSubscriptionNotify" /&gt;  
   &lt;/xs:sequence&gt;  
  &lt;/xs:complexType&gt;  
 &lt;/xs:element&gt;  
 &lt;xs:element name="PreSubscriptionNotify"&gt;  
  &lt;xs:complexType&gt;  
   &lt;xs:sequence&gt;  
    &lt;xs:element ref="MDN" /&gt;  
    &lt;xs:element ref="SPCode" /&gt;  
    &lt;xs:element ref="ProductCode" /&gt;  
    &lt;xs:element ref="TransactionID" /&gt;  
    &lt;xs:element ref="EffectiveDate" /&gt;  
   &lt;/xs:sequence&gt;  
  &lt;/xs:complexType&gt;  
 &lt;/xs:element&gt;  
 &lt;xs:simpleType name="ProductCodeType"&gt;  
  &lt;xs:restriction base="xs:string"&gt;  
   &lt;xs:length value="10" /&gt;  
  &lt;/xs:restriction&gt;  
 &lt;/xs:simpleType&gt;  
 &lt;xs:simpleType name="TransactionIDType"&gt;  
  &lt;xs:restriction base="xs:string"&gt;  
   &lt;xs:length value="40" /&gt;  
  &lt;/xs:restriction&gt;  
 &lt;/xs:simpleType&gt;  
 &lt;xs:element name="ProductCode" type="ProductCodeType"&gt;&lt;/xs:element&gt;  
 &lt;xs:element name="TransactionID" type="TransactionIDType"&gt;&lt;/xs:element&gt;  
 &lt;xs:simpleType name="MDNType"&gt;  
  &lt;xs:restriction base="xs:string"&gt;  
   &lt;xs:length value="11" /&gt;  
   &lt;xs:pattern value="13\[0|1|3]\\d{8}" /&gt;  
  &lt;/xs:restriction&gt;  
 &lt;/xs:simpleType&gt;  
 &lt;xs:element name="MDN" type="MDNType"&gt;&lt;/xs:element&gt;  
 &lt;xs:simpleType name="SPCodeType"&gt;  
  &lt;xs:restriction base="xs:string"&gt;  
   &lt;xs:maxLength value="8" /&gt;  
  &lt;/xs:restriction&gt;  
 &lt;/xs:simpleType&gt;  
 &lt;xs:element name="SPCode" type="SPCodeType"&gt;&lt;/xs:element&gt;  
 &lt;xs:simpleType name="EffectiveDateType"&gt;  
  &lt;xs:restriction base="xs:string"&gt;  
   &lt;xs:length value="19" /&gt;  
   &lt;xs:pattern value="\\d{4}-((0?\[1-9])|(1\[0-2]))-((0?\[1-9])|(\[1-2]\[0-9])|(3\[0-1]))( +)((0?\[0-9])|(1\[0-9])|(2\[0-3])):((0?\[0-9])|(\[1-5]\[0-9])):((0?\[0-9])|(\[1-5]\[0-9]))" /&gt;  
  &lt;/xs:restriction&gt;  
 &lt;/xs:simpleType&gt;  
 &lt;xs:element name="EffectiveDate" type="EffectiveDateType" /&gt;  
&lt;/xs:schema&gt;

方法一:  
File f = new File("d://test.xml");  
InputStream in = new FileInputStream(f);  
String MDN=rot.getChild("U-MAX",org.jdom.Namespace.getNamespace(nsp1)).getChild("PreSubscriptionNotify",org.jdom.Namespace.getNamespace(nsp2)).getChild("MDN",org.jdom.Namespace.getNamespace(nsp2)).getText();  
String SPCode=rot.getChild("U-MAX",org.jdom.Namespace.getNamespace(nsp1)).getChild("PreSubscriptionNotify",org.jdom.Namespace.getNamespace(nsp2)).getChild("SPCode",org.jdom.Namespace.getNamespace(nsp2)).getText();  
String ProductCode=rot.getChild("U-MAX",org.jdom.Namespace.getNamespace(nsp1)).getChild("PreSubscriptionNotify",org.jdom.Namespace.getNamespace(nsp2)).getChild("ProductCode",org.jdom.Namespace.getNamespace(nsp2)).getText();  
String TransactionID=rot.getChild("U-MAX",org.jdom.Namespace.getNamespace(nsp1)).getChild("PreSubscriptionNotify",org.jdom.Namespace.getNamespace(nsp2)).getChild("TransactionID",org.jdom.Namespace.getNamespace(nsp2)).getText();  
String EffectiveDate=rot.getChild("U-MAX",org.jdom.Namespace.getNamespace(nsp1)).getChild("PreSubscriptionNotify",org.jdom.Namespace.getNamespace(nsp2)).getChild("EffectiveDate",org.jdom.Namespace.getNamespace(nsp2)).getText();

方法二：

&lt;?xml version="1.0" encoding="gb2312"?&gt;  
&lt;books&gt;  
  &lt;book email="zhoujunhui"&gt;  
    &lt;name&gt;rjzjh&lt;/name&gt;  
    &lt;price&gt;jjjjjj&lt;/price&gt;  
  &lt;/book&gt;  
&lt;/books&gt;  
 引用的类：  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.Iterator;  
import java.util.List;  
//下面是引用到JDOM中的类  
import org.jdom.Document;  
import org.jdom.Element;  
import org.jdom.JDOMException;  
import org.jdom.input.SAXBuilder;  
import org.jdom.output.XMLOutputter;

1 public class JDomParse {  
2 public JDomParse(){  
3  String xmlpath="bin/library.xml";  
4  SAXBuilder builder=new SAXBuilder(false);  
5  try {  
6   Document doc=builder.build(xmlpath);  
7   Element books=doc.getRootElement();  
8   List booklist=books.getChildren("book");  
9   for (Iterator iter = booklist.iterator(); iter.hasNext();) {  
10    Element book = (Element) iter.next();  
11    String email=book.getAttributeValue("email");  
12    System.out.println(email);  
13    String name=book.getChildTextTrim("name");  
14    System.out.println(name);  
15    book.getChild("name").setText("alterrjzjh");  
16      
17   }  
18     
19   XMLOutputter outputter=new XMLOutputter();  
20   outputter.output(doc,new FileOutputStream(xmlpath));  
21     
22  } catch (JDOMException e) {  
23   e.printStackTrace();  
24  } catch (IOException e) {  
25   e.printStackTrace();  
26  }  
27 }  
28 public static void main(String\[] args) {  
29  new JDomParse();  
30 }  
31}

方法三:

import org.jdom.\*;  
import org.jdom.output.\*;  
import org.jdom.input.\*;  
import java.io.\*;  
import java.util.List;

public class JDomeSample  
{  
public static void main(String\[] args) throws Exception // 如果有任何异常则抛出  
{  
SAXBuilder sb = new SAXBuilder(); // 新建立构造器  
Document doc = sb.build(new FileInputStream("1.xml")); // 读入1.xml  
Element root = doc.getRootElement(); // 取得根节点, 就是例子中的&lt;total&gt;节点  
List list = root.getChildren(); // 取得根节点下一层所有节点放入List类中  
for(int i=0; i&lt;list.size(); i++)  
{  
System.out.println("-------------------------");  
Element item = (Element)list.get(i); // 取得节点实例  
String name = item.getAttribute("name").getValue(); // 取得属性的值  
System.out.println("NAME--&gt;"+name);  
Element sub = item.getChild("sub"); // 取得当前节点的指定子节点  
String text = sub.getText(); // 取得指定子节点的内容  
System.out.println("SUB--&gt;"+text);  
sub.setText("new item"+String.valueOf(i) ); // 改变子节点的内容  
}  
Element item = (Element)list.get(0); // 取得根节点下第一个子节点  
Attribute a = new Attribute("started","true"); // 增加一个新的属性  
item.addAttribute(a);  
item.setAttribute("name","new item"); // 改变旧的属性值  
String indent = ""; // 缩进符号  
boolean newLines = false; // 是否产生新行  
XMLOutputter outp = new XMLOutputter (indent,newLines,"gb2312"); // 构造新的输出流  
outp.output(doc, new FileOutputStream("2.xml")); // 输出到2.XML文件中  
}  
}