一个提取数据库中类型为 XML 的列的内容的程序

Sql Server 2005 中有一个 xml 类型的数据,这种数据的用处是很大的!

以Sql server 2005中自带的AdventureWorks为例: 

Individual 表的类型为 XML 的 Demographics 列包含如下信息,单个客户的常用人口统计信息(如婚姻状况、子女数目、教

育、职业、拥有的汽车数和爱好)等,但是用户可能不会填写全部信息,而为每个统计数据都建立一列对数据库设计来说是很费事

的! 因此,在调查中使用一个类型为 XML 的列,而不是创建多个列(每个问题一列)并在数据库中将未回答的问题存储为值 NULL。

存储为 XML 的调查信息也可以提供给客户关系管理系统和商业智能系统。

因此,建立xml文档格式的列是很有必要的! 

但是:通过jdbc或hibernate读出来的数据还是xml文档类型的,怎样才能取得其中的信息呢?  我想,自己编写一个程序,解析xml文

档,读出其中数据.

我写了一个解析xml文档的程序,这是一个类似于"词法分析器"的程序,但是功能不强,嵌套的xml文档不能分析出来.但是足够满足

功能了.(时间复杂度为n)

java 代码
  1. import java.util.ArrayList;   
  2. import java.util.Iterator;   
  3. import java.util.List;   
  4.   
  5. public class AnalyzeAll {   
  6.      private int currentLength;   
  7.      private int currentPosition;   
  8.      private List list;   
  9.      private XmlPOJO  xp;   
  10.      public AnalyzeAll()   
  11.      {   
  12.          this.setCurrentLength(0);   
  13.          this.setCurrentPosition(0);   
  14.          this.list = new ArrayList();   
  15.          this.xp = new XmlPOJO();   
  16.      }   
  17.      public void analyze(String xml)    
  18.      {   
  19.          this.setCurrentLength(xml.length());   
  20.          while(true){   
  21.          if((this.getCurrentPosition()+2 )>=this.getCurrentLength())   
  22.                  break;     
  23.          if(xml.substring( getCurrentPosition() , 
  24.            getCurrentPosition() + 1).equals("<"))   
  25.          {   
  26.              xp = new XmlPOJO();       
  27.              this.setCurrentPosition(getCurrentPosition()+1);   
  28.              analyzeInside( xml , getCurrentPosition() );   
  29.          }   
  30.          else this.setCurrentPosition(getCurrentPosition()+1);   
  31.          }     
  32.          this.printInfo();   
  33.      }   
  34.      private int analyzeInside(String xml,int cPosition)   
  35.      {     
  36.             
  37.          boolean flag = true;   
  38.          while(true){   
  39.              if((this.getCurrentPosition()+2 )>=this.getCurrentLength())   
  40.                  return 0;   
  41.              if(xml.substring(getCurrentPosition(), 
  42.              getCurrentPosition() + 1).equals(">"))   
  43.                  if(flag)    
  44.                      {   
  45.                      xp.setElementName(xml.substring(cPosition, 
  46.                                        getCurrentPosition()));   
  47.                      this.setCurrentPosition(getCurrentPosition()+1);   
  48.                      this.analyzeProperty(xml, getCurrentPosition());   
  49.                      return 0;   
  50.                      }   
  51.                  else return 0;   
  52.              if(xml.substring(getCurrentPosition(), 
  53.                 getCurrentPosition() + 1).equals(" "))   
  54.                  flag = false;   
  55.              this.setCurrentPosition(this.getCurrentPosition()+1);   
  56.          }   
  57.                 
  58.      }   
  59.         
  60.      private void analyzeProperty(String xml, int cPosition)   
  61.      {   
  62.          while(true){   
  63.              if((this.getCurrentPosition()+2 )>=this.getCurrentLength())   
  64.                  break;   
  65.              if(xml.substring(getCurrentPosition(), 
  66.                 getCurrentPosition() + 2).equals("))   
  67.              {   
  68.                  xp.setProperty(xml.substring(cPosition,getCurrentPosition()));   
  69.                  this.setToList();   
  70.                  break;   
  71.              }   
  72.              this.setCurrentPosition(getCurrentPosition()+1);   
  73.          }   
  74.          while(true){   
  75.              if((this.getCurrentPosition()+2 )>=this.getCurrentLength())   
  76.                  break;   
  77.              if(xml.substring(getCurrentPosition(), 
  78.                 getCurrentPosition() + 1).equals(">"))   
  79.              {   
  80.                  this.setCurrentPosition(getCurrentPosition()+1);          
  81.                  break;   
  82.              }   
  83.              this.setCurrentPosition(getCurrentPosition()+1);   
  84.          }   
  85.      }   
  86.      private void setToList()   
  87.      {   
  88.          list.add(xp);   
  89.      }   
  90.         
  91.      private void printInfo()   
  92.      {   
  93.          Iterator it = list.iterator();   
  94.          for(int i = 0 ; i< list.size() ; i++ )   
  95.          {   
  96.          XmlPOJO xmlpojo = (XmlPOJO)it.next();   
  97.          System.out.println("ElementName: "+xmlpojo.getElementName()+", Property: "+xmlpojo.getProperty());   
  98.          }   
  99.      }   
  100.     public int getCurrentLength() {   
  101.         return currentLength;   
  102.     }   
  103.     public void setCurrentLength(int currentLength) {   
  104.         this.currentLength = currentLength;   
  105.     }   
  106.     public int getCurrentPosition() {   
  107.         return currentPosition;   
  108.     }   
  109.     public void setCurrentPosition(int currentPosition) {   
  110.         this.currentPosition = currentPosition;   
  111.     }   
  112. }  

 其中使用了一个 XmlPOJO的类,用来保存每条 elementName, property.

 

java 代码
  1. public class XmlPOJO {   
  2.      private String elementName;   
  3.      private String property;   
  4.     public String getElementName() {   
  5.         return elementName;   
  6.     }   
  7.     public void setElementName(String elementName) {   
  8.         this.elementName = elementName;   
  9.     }   
  10.     public String getProperty() {   
  11.         return property;   
  12.     }   
  13.     public void setProperty(String property) {   
  14.         this.property = property;   
  15.     }   
  16.         
  17. }  

 

最后,使用的xml用例测试

xml 代码
  1. <IndividualSurvey xmlns=http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey><TotalPurchaseYTD>8248.99TotalPurchaseYTD><DateFirstPurchase>2001-07-22ZDateFirstPurchase><BirthDate>1966-04-08ZBirthDate><MaritalStatus>MMaritalStatus><YearlyIncome>75001-100000YearlyIncome><Gender>MGender><TotalChildren>2TotalChildren><NumberChildrenAtHome>0NumberChildrenAtHome><Education>BachelorsEducation><Occupation>ProfessionalOccupation><HomeOwnerFlag>1HomeOwnerFlag><NumberCarsOwned>0NumberCarsOwned><Hobby>GolfHobby><Hobby>Watch TVHobby><CommuteDistance>1-2 MilesCommuteDistance>IndividualSurvey>  

得出分析结果:

java 代码
  1. ElementName: TotalPurchaseYTD, Property: 8248.99  
  2. ElementName: DateFirstPurchase, Property: 2001-07-22Z   
  3. ElementName: BirthDate, Property: 1966-04-08Z   
  4. ElementName: MaritalStatus, Property: M   
  5. ElementName: YearlyIncome, Property: 75001-100000  
  6. ElementName: Gender, Property: M   
  7. ElementName: TotalChildren, Property: 2  
  8. ElementName: NumberChildrenAtHome, Property: 0  
  9. ElementName: Education, Property: Bachelors   
  10. ElementName: Occupation, Property: Professional   
  11. ElementName: HomeOwnerFlag, Property: 1  
  12. ElementName: NumberCarsOwned, Property: 0  
  13. ElementName: Hobby, Property: Golf   
  14. ElementName: Hobby, Property: Watch TV   
  15. ElementName: CommuteDistance, Property: 1-2 Miles  

即使是10W字节的xml文档也可以在几毫秒的时间内全部分析完毕!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值