以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  [求助]最近在网上看到不少关于解析XML的例子,但例子的XML都是简单的那种,复杂的如何处理?  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=28289)


--  作者:styx
--  发布时间:3/8/2006 2:08:00 PM

--  [求助]最近在网上看到不少关于解析XML的例子,但例子的XML都是简单的那种,复杂的如何处理?
简单的
<?xml version="1.0" encoding="gb2312"?>
<row>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
</row>
复杂的如下面的如何解析?
<?xml version="1.0" encoding="GBK" ?>
<Envelope  type="request"  ConnectionType=“short” ClientID=”身份证编号”>
<XID value="学号"/>
  <Body>
      <Service name=”login” server=” MIS” version=”1.0”>
 <name>王小明</name>
                <college>信息学院</college>
                <telephone>6258113</telephone>
                <notes>男,1955年生,博士,95年调入海南大学</notes>
      </Service>
   </Body>
</Envelope>

--  作者:styx
--  发布时间:3/8/2006 2:15:00 PM

--  
下面的程序只能提取如<xxx>YYYY</xxx>中xxx的内容和YYYY的内容.对于
<Envelope  type="request"  ConnectionType=“short” ClientID=”身份证编号”>中ConnectionType=“short”无法解析.请问有何方法?(偶刚学XML,一般用到啥就找啥,太深了也理解不上)请大家帮看看,谢谢!!!!!

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;

public class OrderProcessor
{
   private static void stepThroughAll(Node start)
   {
      System.out.println(start.getNodeName() + " = " + start.getNodeValue());
      if(start.getNodeType() == start.ELEMENT_NODE)
      {
         NamedNodeMap startAttr = start.getAttributes();
         for(int i = 0; i < startAttr.getLength(); i ++ )
         {
            Node attr = startAttr.item(i);
            System.out.println("   Attribute: " + attr.getNodeName() + " = " + attr.getNodeValue());
         }
      }
      for(Node child = start.getFirstChild(); child != null; child = child.getNextSibling())
      {
         stepThroughAll(child);
      }
   }
   private static void stepThrough(Node start)
   {
      System.out.println(start.getNodeName() + " = " + start.getNodeValue());
      for(Node child = start.getFirstChild(); child != null; child = child.getNextSibling())
      {
         stepThrough(child);
      }
   }
   public static void main(String args[])
   {
      File docFile = new File("Text.xml");
      
      Document doc = null;
      try
      {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         doc = db.parse(docFile);
         Element root = doc.getDocumentElement();
         System.out.println("The root element is " + root.getNodeName());
         NodeList children = root.getChildNodes();
         System.out.println("There are " + children.getLength() + " nodes in this document.");
         //      stepThrough(root);
         stepThroughAll(root);
         for(Node child = root.getFirstChild(); child != null; child = child.getNextSibling())
         {
            System.out.println(child.getNodeName() + " = " + child.getNodeValue());
         }
      }
      catch(Exception e)
      {
         System.out.print("Problem parsing the file: " + e.getMessage());
      }
   }
}


--  作者:styx
--  发布时间:3/8/2006 2:27:00 PM

--  
而且XML中
                <name>王小明</name>
                <college>信息学院</college>
                <telephone>6258113</telephone>
                <notes>男,1955年生,博士,95年调入海南大学</notes>
不一定是四项.有时会是三个或五个<xxx>YYY</xxx>

W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms