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+" ----------");
}
}
}
*/
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+" ----------");
}
}
}
*/