如何用java遍历出xml中每一个attributeValue的值
<?xmlversion="1.0"encoding="GB2312"standalone="yes"?><BroadcastDatacode="110000000"cr...
<?xml version="1.0" encoding="GB2312" standalone="yes"?>
<BroadcastData code="110000000" creationtime="20140315145420" version="2.0">
<ProviderInfo id="TVIBE" name="TVIBE" />
<SchedulerData type="PROGRAM">
<Channel>
<ChannelText language="chi">
<ChannelName>中央少儿</ChannelName>
</ChannelText>
<Event begintime="20140315003100" duration="005100" eventid="0" eventtype="00">
<EventText language="chi">
<Name>神探狄仁杰(25)</Name>
</EventText>
</Event>
<Event begintime="20140315012200" duration="003800" eventid="1" eventtype="11">
<EventText language="chi">
<Name>神探狄仁杰(26)</Name>
</EventText>
</Event>
<Event begintime="20140315020000" duration="020000" eventid="2" eventtype="11">
<EventText language="chi">
<Name>结束</Name>
</EventText>
</Event>
xml格式如上,如何能遍历出每一个Event中的begintime的值,我现在只能读取第一个,后面的就不知道怎么读了 展开
<BroadcastData code="110000000" creationtime="20140315145420" version="2.0">
<ProviderInfo id="TVIBE" name="TVIBE" />
<SchedulerData type="PROGRAM">
<Channel>
<ChannelText language="chi">
<ChannelName>中央少儿</ChannelName>
</ChannelText>
<Event begintime="20140315003100" duration="005100" eventid="0" eventtype="00">
<EventText language="chi">
<Name>神探狄仁杰(25)</Name>
</EventText>
</Event>
<Event begintime="20140315012200" duration="003800" eventid="1" eventtype="11">
<EventText language="chi">
<Name>神探狄仁杰(26)</Name>
</EventText>
</Event>
<Event begintime="20140315020000" duration="020000" eventid="2" eventtype="11">
<EventText language="chi">
<Name>结束</Name>
</EventText>
</Event>
xml格式如上,如何能遍历出每一个Event中的begintime的值,我现在只能读取第一个,后面的就不知道怎么读了 展开
1个回答
2014-12-24
展开全部
递归读取法即可
package tool;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLTool
{
private static final String XML = "MyXml.xml";
private static Document getDoc ( String xml )
{
DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
Document doc = null;
try
{
dbf = DocumentBuilderFactory.newInstance ();
db = dbf.newDocumentBuilder ();
doc = null == xml ? db.newDocument () : db.parse (xml);
return doc;
}
catch (Exception e)
{
e.printStackTrace ();
}
return null;
}
private static void readXML ( String xml, String... looks )
{
Document doc = getDoc (xml);
recursionXML (doc, looks);
}
private static void recursionXML ( Node node, String... looks )
{
NodeList nl = node.getChildNodes ();
if (nl.getLength () == 0)
{
return;
}
for ( int i = 0; i < nl.getLength (); i++ )
{
Node n = nl.item (i);
Node parentNode = n.getParentNode ();
NamedNodeMap nnm = parentNode.getAttributes ();
if (
looks.length != 0
&&
parentNode.getNodeName ().equals (looks[0])
&&
nnm.getNamedItem (looks[1]).getNodeValue ().equals (looks[2])
)
{
System.out.println (nnm.getNamedItem ("Status"));
}
recursionXML (n, looks);
}
}
public static void main ( String[] args )
{
System.out.println ("输入id号进行查询: (n/N 退出)");
Scanner scanner = new Scanner (System.in);
String[] looks = { "UseTicketResult", "TicketNo", "47" };
String line = null;
while (scanner.hasNextLine ())
{
line = scanner.nextLine ().trim ();
if ("n".equalsIgnoreCase (line))
{
scanner.close ();
break;
}
looks[2] = line;
readXML (XML, looks);
System.out.println ("继续输入: ");
}
}
}
package tool;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLTool
{
private static final String XML = "MyXml.xml";
private static Document getDoc ( String xml )
{
DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
Document doc = null;
try
{
dbf = DocumentBuilderFactory.newInstance ();
db = dbf.newDocumentBuilder ();
doc = null == xml ? db.newDocument () : db.parse (xml);
return doc;
}
catch (Exception e)
{
e.printStackTrace ();
}
return null;
}
private static void readXML ( String xml, String... looks )
{
Document doc = getDoc (xml);
recursionXML (doc, looks);
}
private static void recursionXML ( Node node, String... looks )
{
NodeList nl = node.getChildNodes ();
if (nl.getLength () == 0)
{
return;
}
for ( int i = 0; i < nl.getLength (); i++ )
{
Node n = nl.item (i);
Node parentNode = n.getParentNode ();
NamedNodeMap nnm = parentNode.getAttributes ();
if (
looks.length != 0
&&
parentNode.getNodeName ().equals (looks[0])
&&
nnm.getNamedItem (looks[1]).getNodeValue ().equals (looks[2])
)
{
System.out.println (nnm.getNamedItem ("Status"));
}
recursionXML (n, looks);
}
}
public static void main ( String[] args )
{
System.out.println ("输入id号进行查询: (n/N 退出)");
Scanner scanner = new Scanner (System.in);
String[] looks = { "UseTicketResult", "TicketNo", "47" };
String line = null;
while (scanner.hasNextLine ())
{
line = scanner.nextLine ().trim ();
if ("n".equalsIgnoreCase (line))
{
scanner.close ();
break;
}
looks[2] = line;
readXML (XML, looks);
System.out.println ("继续输入: ");
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询