如何在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。第二个只给出null值。
我需要我的结果:
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
答案 0 :(得分:0)
由于您还没有真正说出要对XML文件做什么,我能做的最好的就是指导您阅读本指南:
答案 1 :(得分:0)
您可以使用JAXP来解析XML
答案 2 :(得分:0)
更好/更容易不在Java中执行此操作,而是在可以从Java应用程序调用的XSLT或XQuery代码中执行此操作。