一种简单环境下,读取XML节点值的方法

 

import  javax.xml.parsers. * ;
import  org.w3c.dom. * ;

public   class   XMLTest
{
 
public static void main(String[] args) 
 

  Document doc 
= null;
  
try{
   DocumentBuilderFactory factory 
= DocumentBuilderFactory.newInstance();
   DocumentBuilder builder
=factory.newDocumentBuilder();
   doc 
= builder.parse("chicang.xml");
   doc.normalize();
  }
catch(Exception e){System.out.println(e);}
  
//System.out.println(doc.getDocumentElement().getNodeName());
  
//根节点为n0,第一层为n1,第二层为n2,依此类推
  /*
  NodeList n1 = doc.getDocumentElement().getChildNodes();
  
  NodeList[] n2 = NodePlus.getSubList(n1);
  
  NodeList[] n3 = NodePlus.getSubList(n2[0]);
  
*/

  
//System.out.println(n2[0].getLength());
  
//NodePlus.print(n3);

  
//NodeList n2 = GetNodes.getChildList(n1);

  NodeList nv 
= doc.getElementsByTagName("value");
  
//NodeList nk = doc.getElementsByTagName("key");

  String[] v 
= new String[nv.getLength()];
  
//String[] k = new String[nk.getLength()];

  
for(int i=0; i<nv.getLength(); i++){
   
//k[i] = nk.item(i).getFirstChild().getNodeValue();
   v[i] = nv.item(i).getFirstChild().getNodeValue();
   
//System.out.println(k[i]+":"+v[i]);
  }


  
//int num_stock = (nv.getLength() - 6)/5 ;

  String[] stockinfo 
= new String[nv.getLength()-6];
  String[] accountinfo 
= new String[3];
  String[] money 
= new String[3];
  
  
for(int i=6,j=1; i<v.length; i++){
   stockinfo[i
-6= v[i];
   System.out.println(stockinfo[i
-6]);
   
if(i%5 == 0){
    System.out.println(
"---------- end of stock "+j+" --------");
    j
++;
   }

  }


  
for(int i=0; i<3; i++){
   accountinfo[i] 
= v[i];
   System.out.println(accountinfo[i]);
  }

  System.out.println(
"---------- end of accountinfo --------");

  
for(int i=3; i<6; i++){
   money[i
-3= v[i];
   System.out.println(money[i
-3]);
  }

  System.out.println(
"---------- end of money --------");
  
 }

}


/*

class NodePlus

 //获得某NodeList的各元素下一层NodeList,包含无效节点,存为NodeList[]
 public static NodeList[] getSubList(NodeList nl){
  NodeList[] nList = new NodeList[getNum(nl)];
  int j=0;
  for(int i=0; i<nl.getLength(); i++){
   if(nl.item(i).getNodeType() == Node.ELEMENT_NODE){
    //System.out.println(nl.item(i).getNodeName());
    nList[j] = nl.item(i).getChildNodes();
    j++;
   }
  }
  return nList;
 }

 //获得节点列表的有效节点数,如果为0则表示上级无子节点,到达最底层
 public static int getNum(NodeList nl){
  int num = 0;
  for(int i=0; i<nl.getLength(); i++){
   if(nl.item(i).getNodeType() == Node.ELEMENT_NODE){
    num ++;
   }
  }
  return num;
 }

 //打印NodeList各节点名称
 public static void print(NodeList nl){
  for(int i=0; i<nl.getLength(); i++){
   if(nl.item(i).getNodeType() == Node.ELEMENT_NODE){
    System.out.println(nl.item(i).getNodeName());
   }
  }
 }

 //打印NodeList[]各节点名称,分隔各组NodeList
 public static void print(NodeList[] nList){
  for(int i=0; i<nList.length; i++){
   print(nList[i]);
   System.out.println("---------- End of Group "+i+" ----------");
  }
 }

}

*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值