如何在 java 中读取 XML 文件。
下面是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LivescoreData>
<Sport SportId="1">
<Name language="en">Soccer</Name>
<Name language="se">Fotboll</Name>
<Category CategoryId="34">
<Name language="en">Australia</Name>
<Name language="se">Australien</Name>
<Tournament TournamentId="144">
<Name language="en">Hyundai A-League</Name>
<Name language="se">Hyundai A-League</Name>
<Match MatchId="4616735">
<MatchDate>2011-01-05T07:30:00</MatchDate>
<Team1 TeamId="1029369">
<Name language="en">Wellington Phoenix FC</Name>
<Name language="se">Wellington</Name>
</Team1>
<Team2 TeamId="529088">
<Name language="en">Melbourne Victory</Name>
<Name language="se">Melbourne Victory</Name>
</Team2>
<Status Code="100">
<Name language="en">Ended</Name>
<Name language="se">Avslutad</Name>
</Status>
<Winner>1</Winner>
<Scores>
<Score type="Current">
<Team1>2</Team1>
<Team2>0</Team2>
</Score>
</Scores>
<Goals></Goals>
<Cards></Cards>
<Substitutions></Substitutions>
<Lineups></Lineups>
</Match>
</Tournament>
</Category>
<Category CategoryId="1">
<Name language="en">England</Name>
<Name language="se">England</Name>
<Tournament TournamentId="1">
<Name language="en">Premier League</Name>
<Name language="se">Premier League</Name>
<Match MatchId="4601857">
<MatchDate>2011-01-04T21:00:00</MatchDate>
<Team1 TeamId="5431228">
<Name language="en">Blackpool FC</Name>
<Name language="se">Blackpool FC</Name>
</Team1>
<Team2 TeamId="23960">
<Name language="en">Birmingham City</Name>
<Name language="se">Birmingham City</Name>
</Team2>
<Status Code="100">
<Name language="en">Ended</Name>
<Name language="se">Avslutad</Name>
</Status>
<Winner>1</Winner>
<Scores>
<Score type="Current">
<Team1>5</Team1>
<Team2>1</Team2>
</Score>
</Scores>
<Goals></Goals>
<Cards></Cards>
<Substitutions></Substitutions>
<Lineups></Lineups>
</Match>
<Match MatchId="4601859">
<MatchDate>2011-01-04T21:00:00</MatchDate>
<Team1 TeamId="26511">
<Name language="en">Fulham FC</Name>
<Name language="se">Fulham FC</Name>
</Team1>
<Team2 TeamId="94356">
<Name language="en">West Bromwich Albion</Name>
<Name language="se">West Bromwich Albion</Name>
</Team2>
<Status Code="100">
<Name language="en">Ended</Name>
<Name language="se">Avslutad</Name>
</Status>
<Winner>1</Winner>
<Scores>
<Score type="Current">
<Team1>4</Team1>
<Team2>1</Team2>
</Score>
</Scores>
<Goals></Goals>
<Cards></Cards>
<Substitutions></Substitutions>
<Lineups></Lineups>
</Match>
</Tournament>
</Category>
</Sport>
</LivescoreData>
下面是代码:IT 打印 XML Soccer 的第一个值并且无法打印下一个值。
nodeLst = doc.getElementsByTagName("Sport");
for (int i = 0; i < nodeLst.getLength(); i++) {
Node myNode = nodeLst.item(i);
if (myNode.getNodeType() == Node.ELEMENT_NODE) {
Element Sport = (Element) myNode;
NodeList Name= Sport
.getElementsByTagName("Name");
Element NameElement = (Element) Name.item(0);
NodeList Namevalue = NameElement.getChildNodes();
System.out.println("Name : "
+ ((Node) Namevalue.item(0)).getNodeValue()+"|");
//This gives me null value
NodeList Category = Sport
.getElementsByTagName("Category");
Element CategoryName= (Element) Category
.item(0);
NodeList Categoryvalue = CategoryName
.getChildNodes();
System.out.println("Category: "
+ ((Node) Categoryvalue.item(0))
.getNodeValue());
}
}
我只能读取第一个数据足球。第二个只给出空值。
我需要我的结果:
Soccer | Australia | Hyundai A-League | Wellington Phoenix FC - Melbourne Victory : 2 - 0
Soccer | England | Premier League | Blackpool FC - Birmingham City : 5 - 1
Soccer | England | Premier League | Fulham FC - West Bromwich Albion : 4 - 1