如何用java取得XML节点的属性值

<?xml version="1.0" encoding="gbk" standalone="no" ?>
<result>
<instance waveuri="e:\temp\333.wav" fmt="alaw" sample-rate="8000" bit-rate="8" channel="mono" duration="358339">
<subject value="speaker-separation">
<param reserved="" />
<channel no="n0" channeluri="mono">
<items count="6" duration="22160">
<item start="9400" end="12340" />
<item start="13020" end="14520" />
<item start="15310" end="18630" />
<item start="337980" end="341410" />
<item start="341940" end="343960" />
<item start="348790" end="357740" />
</items>
</channel>
</subject>
<subject value="search">
<channel no="n0">
<function value="1-best" count="50" speed="3.9">

</function>
<function value="long-silence">
<param duration="10000"/>
<items count="1" duration="319350">
<item start="18630" end="337980" duration="319350" />
</items>
</function>
</channel>
</subject>
</instance>
</result>

如何取到 <item start="18630" end="337980" duration="319350" />
中得START的值

第1个回答  2011-08-15
下个dom4j包我下面的方法笨了点不知道还有好的不

import java.io.File;
import java.util.Iterator;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

public class ResultXmlTest1 {
public static void main(String args[])
{
SAXReader reader=new SAXReader();
try
{
Document doc=reader.read(new File("result.xml"));
Element root=doc.getRootElement();
for(Iterator ite=root.elementIterator();ite.hasNext();)
{
Element ele=(Element)ite.next();
for(Iterator iter=ele.elementIterator();iter.hasNext();)
{
Element elem=(Element)iter.next();
//System.out.println(elem.getName());
for(Iterator itera=elem.elementIterator();itera.hasNext();)
{
Element eleme=(Element)itera.next();
//System.out.println(eleme.getName());
for(Iterator iterat=eleme.elementIterator();iterat.hasNext();)
{
Element elemem=(Element)iterat.next();
//System.out.println(elemem.getName());
for(Iterator iterato=elemem.elementIterator();iterato.hasNext();)
{
Element elememe=(Element)iterato.next();
//System.out.println(elememe.getName());
for(Iterator last=elememe.elementIterator();last.hasNext();)
{
Element elemLast=(Element)last.next();
//System.out.println(elemLast.getName());
for(Iterator at=elemLast.attributeIterator();at.hasNext();)
{
Attribute att=(Attribute)at.next();
if(att.getName().equals("start"))
System.out.println(att.getText());
}
}
}
}
}
}

}
}catch(DocumentException e)
{
e.printStackTrace();
}
}

}
第2个回答  2011-08-03
网上用JAVA解析XML的方法有四种
JAXP、SAX、JDOM、DOM4J四种方式、 建议你使用jdom或者dom4j、网上有很多现成的例子,你可以去参考一下!
第3个回答  2011-08-03
解析的方法有很多,比如dom4j
相似回答