`
mhbjava
  • 浏览: 95856 次
最近访客 更多访客>>
社区版块
存档分类
最新评论
阅读更多

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

2006.3.21
解析xml

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

test.xsl:

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


方法一:
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();


方法二:


<?xml version="1.0" encoding="gb2312"?>
<books>
  <book email="zhoujunhui">
    <name>rjzjh</name>
    <price>jjjjjj</price>
  </book>
</books>
 引用的类:
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(); // 取得根节点, 就是例子中的<total>节点
List list = root.getChildren(); // 取得根节点下一层所有节点放入List类中
for(int i=0; i<list.size(); i++)
{
System.out.println("-------------------------");
Element item = (Element)list.get(i); // 取得节点实例
String name = item.getAttribute("name").getValue(); // 取得属性的值
System.out.println("NAME-->"+name);
Element sub = item.getChild("sub"); // 取得当前节点的指定子节点
String text = sub.getText(); // 取得指定子节点的内容
System.out.println("SUB-->"+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文件中
}




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics