我有一个存储路径为D:\collectInfo.xml的文件我要把里面每一项都去出来,以便于其他用途
collectInfo.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <collectInfo>
- <collectDes>
<infoSrcType>信息源类型</infoSrcType>
<srcName>信息源名称</srcName>
- <tags>
<tag>收集字段0</tag>
<tag>收集字段01</tag>
</tags>
</collectDes>
- <collectDes>
<infoSrcType>信息源类型1111111111</infoSrcType>
<srcName>信息源名称11111111</srcName>
- <tags>
<tag>收集字段0000011111</tag>
<tag>收集字段0000011111</tag>
</tags>
</collectDes>
- <collectDes>
<infoSrcType>信息源类型2222222</infoSrcType>
<srcName>信息源名称2222222</srcName>
- <tags>
<tag>收集字段2222222211111</tag>
<tag>收集字段2222222211111</tag>
</tags>
</collectDes>
</collectInfo>
接下来,我写了一个CollectInfoBean.java用于组装我取出来的collectInfo.xml中的节点值.
CollectInfoBean.java如下:
public class CollectInfoBean
{
private String infoSrcType; // the type of the source of the information
private String srcName; // the name of the soutce of the information
private String[] tag; //the items we need to collect
public String[] getTag()
{
return tag;
}
public void setTag(String[] tag)
{
this.tag = tag;
}
public String getInfoSrcType()
{
return infoSrcType;
}
public void setInfoSrcType(String infoSrcType)
{
this.infoSrcType = infoSrcType;
}
public String getSrcName()
{
return srcName;
}
public void setSrcName(String srcName)
{
this.srcName = srcName;
}
}
然后我写了一个解析collectInfo.xml的ParseCollectInfoXML.java文件如下:
ParseCollectInfoXML.javaimport java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ParseCollectInfoXML
{
public static ArrayList getCollectInfo(String uri)
{
ArrayList list = new ArrayList();
try
{
DocumentBuilderFactory DBfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder DBbuilder = DBfactory.newDocumentBuilder();
Document doc = DBbuilder.parse(uri);
NodeList nodeList = doc.getElementsByTagName("collectDes");
String tempString[];
int tempVar = 0;
for(int i = 0 ; i < nodeList.getLength(); i++)
{
String infoSrcType = doc.getElementsByTagName("infoSrcType").item(i).getFirstChild().getNodeValue();
String srcName = doc.getElementsByTagName("srcName").item(i).getFirstChild().getNodeValue();
CollectInfoBean collectInfo = new CollectInfoBean();
collectInfo.setInfoSrcType(infoSrcType);
collectInfo.setSrcName(srcName); //就是这一块取得各个<tags>下面的<tag>时出错,请高手指教,谢谢!!!!
//问题开始点
Node nodeTags = doc.getElementsByTagName("tags").item(i);
for(Node nodeTag = nodeTags.getFirstChild();nodeTag!=null;nodeTag=nodeTag.getNextSibling())
{
tempString[var++] = nodeTag.getFirstChild().getNextSibling();
}
collectInfo.setTag(tempString) ;
//问题结束点 }
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
}
public static void main(String[] args)
{
try
{
String uri = "D:\\collectInfo.xml";
ArrayList list = getCollectInfo(uri);
try
{
for(int count= 0 ; count<list.size();count++ )
{
CollectInfoBean temp = (CollectInfoBean)list.get(count);
System.out.println("第" +( count+1)+"条信息--->");
if(temp.getInfoSrcType()!=null)
{
System.out.println("信息源类型:"+temp.getInfoSrcType());
}
if(temp.getSrcName()!=null)
{
System.out.println("信息源名称:"+temp.getSrcName());
}
if(temp.getTag().length>0)
{
int i = temp.getTag().length;
String[] sStemp =temp.getTag();
for(int j =0;j<i;j++)
{
System.out.println("收集信息为:"+sStemp[j]);
}
}
System.out.println("-----------------------------------------");
}
}
catch(Exception e)
{
System.out.println("Exception is occurrented ---->"+e.getMessage());
System.out.println(e.getStackTrace());
}
System.out.println("所有信息已经读出.");
}
catch(FactoryConfigurationError e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}但是,我得不到各个<tags>下面的<tag>的值,希望您能帮忙!!!要是可以的话,希望您能把答案发到我E_mail:[email protected] 谢谢!!!
collectInfo.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <collectInfo>
- <collectDes>
<infoSrcType>信息源类型</infoSrcType>
<srcName>信息源名称</srcName>
- <tags>
<tag>收集字段0</tag>
<tag>收集字段01</tag>
</tags>
</collectDes>
- <collectDes>
<infoSrcType>信息源类型1111111111</infoSrcType>
<srcName>信息源名称11111111</srcName>
- <tags>
<tag>收集字段0000011111</tag>
<tag>收集字段0000011111</tag>
</tags>
</collectDes>
- <collectDes>
<infoSrcType>信息源类型2222222</infoSrcType>
<srcName>信息源名称2222222</srcName>
- <tags>
<tag>收集字段2222222211111</tag>
<tag>收集字段2222222211111</tag>
</tags>
</collectDes>
</collectInfo>
接下来,我写了一个CollectInfoBean.java用于组装我取出来的collectInfo.xml中的节点值.
CollectInfoBean.java如下:
public class CollectInfoBean
{
private String infoSrcType; // the type of the source of the information
private String srcName; // the name of the soutce of the information
private String[] tag; //the items we need to collect
public String[] getTag()
{
return tag;
}
public void setTag(String[] tag)
{
this.tag = tag;
}
public String getInfoSrcType()
{
return infoSrcType;
}
public void setInfoSrcType(String infoSrcType)
{
this.infoSrcType = infoSrcType;
}
public String getSrcName()
{
return srcName;
}
public void setSrcName(String srcName)
{
this.srcName = srcName;
}
}
然后我写了一个解析collectInfo.xml的ParseCollectInfoXML.java文件如下:
ParseCollectInfoXML.javaimport java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ParseCollectInfoXML
{
public static ArrayList getCollectInfo(String uri)
{
ArrayList list = new ArrayList();
try
{
DocumentBuilderFactory DBfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder DBbuilder = DBfactory.newDocumentBuilder();
Document doc = DBbuilder.parse(uri);
NodeList nodeList = doc.getElementsByTagName("collectDes");
String tempString[];
int tempVar = 0;
for(int i = 0 ; i < nodeList.getLength(); i++)
{
String infoSrcType = doc.getElementsByTagName("infoSrcType").item(i).getFirstChild().getNodeValue();
String srcName = doc.getElementsByTagName("srcName").item(i).getFirstChild().getNodeValue();
CollectInfoBean collectInfo = new CollectInfoBean();
collectInfo.setInfoSrcType(infoSrcType);
collectInfo.setSrcName(srcName); //就是这一块取得各个<tags>下面的<tag>时出错,请高手指教,谢谢!!!!
//问题开始点
Node nodeTags = doc.getElementsByTagName("tags").item(i);
for(Node nodeTag = nodeTags.getFirstChild();nodeTag!=null;nodeTag=nodeTag.getNextSibling())
{
tempString[var++] = nodeTag.getFirstChild().getNextSibling();
}
collectInfo.setTag(tempString) ;
//问题结束点 }
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
}
public static void main(String[] args)
{
try
{
String uri = "D:\\collectInfo.xml";
ArrayList list = getCollectInfo(uri);
try
{
for(int count= 0 ; count<list.size();count++ )
{
CollectInfoBean temp = (CollectInfoBean)list.get(count);
System.out.println("第" +( count+1)+"条信息--->");
if(temp.getInfoSrcType()!=null)
{
System.out.println("信息源类型:"+temp.getInfoSrcType());
}
if(temp.getSrcName()!=null)
{
System.out.println("信息源名称:"+temp.getSrcName());
}
if(temp.getTag().length>0)
{
int i = temp.getTag().length;
String[] sStemp =temp.getTag();
for(int j =0;j<i;j++)
{
System.out.println("收集信息为:"+sStemp[j]);
}
}
System.out.println("-----------------------------------------");
}
}
catch(Exception e)
{
System.out.println("Exception is occurrented ---->"+e.getMessage());
System.out.println(e.getStackTrace());
}
System.out.println("所有信息已经读出.");
}
catch(FactoryConfigurationError e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}但是,我得不到各个<tags>下面的<tag>的值,希望您能帮忙!!!要是可以的话,希望您能把答案发到我E_mail:[email protected] 谢谢!!!
{
ArrayList list = new ArrayList();
try {
DocumentBuilderFactory DBfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder DBbuilder = DBfactory.newDocumentBuilder();
Document doc = DBbuilder.parse(uri);
NodeList nodeList = doc.getElementsByTagName("collectDes");
for(int i = 0; i<nodeList.getLength(); i++)
{
String infoSrcType = doc.getElementsByTagName("infoSrcType").item(i).getFirstChild().getNodeValue();
String srcName = doc.getElementsByTagName("srcName").item(i).getFirstChild().getNodeValue();
CollectInfoBean collectInfo = new CollectInfoBean();
collectInfo.setInfoSrcType(infoSrcType);
collectInfo.setSrcName(srcName);
//就是这一块取得各个 <tags> 下面的 <tag> 时出错,请高手指教,谢谢!!!!
//问题开始点
Node nodeTags = doc.getElementsByTagName("tags").item(i);
NodeList tagList = nodeTags.getChildNodes();
int tagCount = tagList.getLength();
if( tagCount>0 )
{
int elemCount = 0, index=0;
for(index=0; index<tagCount; index++)
{
if(Node.ELEMENT_NODE==tagList.item(index).getNodeType())
++elemCount; //计算要提取的文本行数量
}
String[] tempString = new String[elemCount];
for(index=0,elemCount=0; index<tagCount; index++)
{
if(Node.ELEMENT_NODE==tagList.item(index).getNodeType())
{
tempString[elemCount++] = tagList.item(index).getFirstChild().getNodeValue();
}
}
collectInfo.setTag(tempString);
}
//问题结束点
list.add(collectInfo);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
}
你用的SUN自带的来解析XML.它对换行啊,什么的都没有去掉的处理,如果你直接打印会发现 #之类的东西.用SAX就不会出现这种现象..
// 问题开始点
tempString=new String[2];
tempVar=0;
Node nodeTags = doc.getElementsByTagName("tags").item(i);
for (Node nodeTag = nodeTags.getFirstChild(); nodeTag != null; nodeTag = nodeTag.getNextSibling()) {
if(nodeTag instanceof Element){
Element element=(Element)nodeTag;
Text textNode=(Text)element.getFirstChild();
String text=textNode.getData();
tempString[tempVar++] =text;
}
}
collectInfo.setTag(tempString) ;
list.add(collectInfo);
// 问题结束点