Java 如何解析这样的文档?
我有一个问题,如果有人能帮我解决,那将是非常感谢的:) 我正在尝试解析此XML文件:Java 如何解析这样的文档?,java,android,xml,parsing,saxparser,Java,Android,Xml,Parsing,Saxparser,我有一个问题,如果有人能帮我解决,那将是非常感谢的:) 我正在尝试解析此XML文件: <data> <day> <match> <team1>foo</team1> <team2>foo</team2> <resultfinal></resultfinal> &
<data>
<day>
<match>
<team1>foo</team1>
<team2>foo</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<venue>1</venue>
<gmt>10:00</gmt>
<groupe>B</groupe>
</match>
<match>
<team1>foo</team1>
<team2>foo</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<venue>1</venue>
<gmt>14:00</gmt>
<groupe>A</groupe>
</match>
</day>
<day>
<match>
<team1>foo</team1>
<team2>foo</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<venue>1</venue>
<gmt>10:00</gmt>
<groupe>B</groupe>
</match>
<match>
<team1>foo</team1>
<team2>foo</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<venue>1</venue>
<gmt>14:00</gmt>
<groupe>A</groupe>
</match>
</day>
</data>
<data>
<day>
<match>
<team1>ind</team1>
<team2>afg</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>19.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>eng</team1>
<team2>afg</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>21.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>ind</team1>
<team2>eng</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>23.09</date>
<venue>2</venue>
<gmt>10:00</gmt>
</match>
</day>
<day>
<match>
<team1>aust</team1>
<team2>irl</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>19.08</date>
<venue>1</venue>
<gmt>10:00</gmt>
</match>
<match>
<team1>aust</team1>
<team2>west</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>22.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>west</team1>
<team2>irl</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>24.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
</day>
</data>
但是有了它,我只能像第二个一样解析文件。如何解析第一个文件以及如何存储数据?我无法将其存储在Arraylist中
编辑:
我试图按照Don Roby的建议构建它,但我的应用程序总是崩溃。以下是我迄今为止所做的:
main活动:
public class MainActivity extends Activity {
XMLHandler data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Log.i("PARSER", "versuch!");
/**
* Create a new instance of the SAX parser
**/
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("my xml file"); // URL of the XML
/**
* Create the Handler to handle each of the XML tags.
**/
data = new XMLHandler();
xmlR.setContentHandler(data);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
Log.i("PARSER", "ES KLAPPT NICHT!");
}
TextView txt1 = (TextView) findViewById(R.id.txtview1);
txt1.setText(data.getTournament().get(0).get(0).getTeam1().toString());
}
public class XMLHandler extends DefaultHandler {
private Tournament tournament;
private TournamentDay currentDay;
private Match currentMatch;
private StringBuilder builder;
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("team1"))
currentMatch.setTeam1(builder.toString());
else if (qName.equalsIgnoreCase("team2"))
currentMatch.setTeam2(builder.toString());
else if (qName.equalsIgnoreCase("resultfinal"))
currentMatch.setResultfinal(builder.toString());
else if (qName.equalsIgnoreCase("result1"))
currentMatch.setResult1(builder.toString());
else if (qName.equalsIgnoreCase("result2"))
currentMatch.setResult2(builder.toString());
else if (qName.equalsIgnoreCase("venue"))
currentMatch.setVenue(builder.toString());
else if (qName.equalsIgnoreCase("gmt"))
currentMatch.setGmt(builder.toString());
else if (qName.equals("match"))
currentDay.add(currentMatch);
else if (qName.equals("day"))
tournament.add(currentDay);
}
@Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
if (qName.equals("data")) {
tournament = new Tournament();
}
if (qName.equals("day")) {
currentDay = new TournamentDay();
}
else if (qName.equals("match")) {
currentMatch = new Match();
}
else {
builder = new StringBuilder();
}
}
@Override
public void characters(char[] chars, int start, int length) throws SAXException {
builder.append(chars, start, length);
}
public Tournament getTournament() {
return tournament;
}
}
public class Tournament {
private List<TournamentDay> days;
public Tournament() {
this.days = new ArrayList<TournamentDay>();
}
public void add(TournamentDay day) {
days.add(day);
}
public TournamentDay get(int i) {
return days.get(i);
}
}
public class Match {
private String team1;
private String team2;
private String resultfinal;
private String result1;
private String result2;
private String date;
private String venue;
private String gmt;
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getResultfinal() {
return resultfinal;
}
public void setResultfinal(String resultfinal) {
this.resultfinal = resultfinal;
}
public String getResult1() {
return result1;
}
public void setResult1(String result1) {
this.result1 = result1;
}
public String getResult2() {
return result2;
}
public void setResult2(String result2) {
this.result2 = result2;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
public String getGmt() {
return gmt;
}
public void setGmt(String gmt) {
this.gmt = gmt;
}
}
XMLHandler:
public class MainActivity extends Activity {
XMLHandler data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Log.i("PARSER", "versuch!");
/**
* Create a new instance of the SAX parser
**/
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("my xml file"); // URL of the XML
/**
* Create the Handler to handle each of the XML tags.
**/
data = new XMLHandler();
xmlR.setContentHandler(data);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
Log.i("PARSER", "ES KLAPPT NICHT!");
}
TextView txt1 = (TextView) findViewById(R.id.txtview1);
txt1.setText(data.getTournament().get(0).get(0).getTeam1().toString());
}
public class XMLHandler extends DefaultHandler {
private Tournament tournament;
private TournamentDay currentDay;
private Match currentMatch;
private StringBuilder builder;
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("team1"))
currentMatch.setTeam1(builder.toString());
else if (qName.equalsIgnoreCase("team2"))
currentMatch.setTeam2(builder.toString());
else if (qName.equalsIgnoreCase("resultfinal"))
currentMatch.setResultfinal(builder.toString());
else if (qName.equalsIgnoreCase("result1"))
currentMatch.setResult1(builder.toString());
else if (qName.equalsIgnoreCase("result2"))
currentMatch.setResult2(builder.toString());
else if (qName.equalsIgnoreCase("venue"))
currentMatch.setVenue(builder.toString());
else if (qName.equalsIgnoreCase("gmt"))
currentMatch.setGmt(builder.toString());
else if (qName.equals("match"))
currentDay.add(currentMatch);
else if (qName.equals("day"))
tournament.add(currentDay);
}
@Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
if (qName.equals("data")) {
tournament = new Tournament();
}
if (qName.equals("day")) {
currentDay = new TournamentDay();
}
else if (qName.equals("match")) {
currentMatch = new Match();
}
else {
builder = new StringBuilder();
}
}
@Override
public void characters(char[] chars, int start, int length) throws SAXException {
builder.append(chars, start, length);
}
public Tournament getTournament() {
return tournament;
}
}
public class Tournament {
private List<TournamentDay> days;
public Tournament() {
this.days = new ArrayList<TournamentDay>();
}
public void add(TournamentDay day) {
days.add(day);
}
public TournamentDay get(int i) {
return days.get(i);
}
}
public class Match {
private String team1;
private String team2;
private String resultfinal;
private String result1;
private String result2;
private String date;
private String venue;
private String gmt;
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getResultfinal() {
return resultfinal;
}
public void setResultfinal(String resultfinal) {
this.resultfinal = resultfinal;
}
public String getResult1() {
return result1;
}
public void setResult1(String result1) {
this.result1 = result1;
}
public String getResult2() {
return result2;
}
public void setResult2(String result2) {
this.result2 = result2;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
public String getGmt() {
return gmt;
}
public void setGmt(String gmt) {
this.gmt = gmt;
}
}
锦标赛:
public class MainActivity extends Activity {
XMLHandler data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Log.i("PARSER", "versuch!");
/**
* Create a new instance of the SAX parser
**/
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("my xml file"); // URL of the XML
/**
* Create the Handler to handle each of the XML tags.
**/
data = new XMLHandler();
xmlR.setContentHandler(data);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
Log.i("PARSER", "ES KLAPPT NICHT!");
}
TextView txt1 = (TextView) findViewById(R.id.txtview1);
txt1.setText(data.getTournament().get(0).get(0).getTeam1().toString());
}
public class XMLHandler extends DefaultHandler {
private Tournament tournament;
private TournamentDay currentDay;
private Match currentMatch;
private StringBuilder builder;
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("team1"))
currentMatch.setTeam1(builder.toString());
else if (qName.equalsIgnoreCase("team2"))
currentMatch.setTeam2(builder.toString());
else if (qName.equalsIgnoreCase("resultfinal"))
currentMatch.setResultfinal(builder.toString());
else if (qName.equalsIgnoreCase("result1"))
currentMatch.setResult1(builder.toString());
else if (qName.equalsIgnoreCase("result2"))
currentMatch.setResult2(builder.toString());
else if (qName.equalsIgnoreCase("venue"))
currentMatch.setVenue(builder.toString());
else if (qName.equalsIgnoreCase("gmt"))
currentMatch.setGmt(builder.toString());
else if (qName.equals("match"))
currentDay.add(currentMatch);
else if (qName.equals("day"))
tournament.add(currentDay);
}
@Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
if (qName.equals("data")) {
tournament = new Tournament();
}
if (qName.equals("day")) {
currentDay = new TournamentDay();
}
else if (qName.equals("match")) {
currentMatch = new Match();
}
else {
builder = new StringBuilder();
}
}
@Override
public void characters(char[] chars, int start, int length) throws SAXException {
builder.append(chars, start, length);
}
public Tournament getTournament() {
return tournament;
}
}
public class Tournament {
private List<TournamentDay> days;
public Tournament() {
this.days = new ArrayList<TournamentDay>();
}
public void add(TournamentDay day) {
days.add(day);
}
public TournamentDay get(int i) {
return days.get(i);
}
}
public class Match {
private String team1;
private String team2;
private String resultfinal;
private String result1;
private String result2;
private String date;
private String venue;
private String gmt;
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getResultfinal() {
return resultfinal;
}
public void setResultfinal(String resultfinal) {
this.resultfinal = resultfinal;
}
public String getResult1() {
return result1;
}
public void setResult1(String result1) {
this.result1 = result1;
}
public String getResult2() {
return result2;
}
public void setResult2(String result2) {
this.result2 = result2;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
public String getGmt() {
return gmt;
}
public void setGmt(String gmt) {
this.gmt = gmt;
}
}
最后是我的XML文件:
<data>
<day>
<match>
<team1>foo</team1>
<team2>foo</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<venue>1</venue>
<gmt>10:00</gmt>
<groupe>B</groupe>
</match>
<match>
<team1>foo</team1>
<team2>foo</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<venue>1</venue>
<gmt>14:00</gmt>
<groupe>A</groupe>
</match>
</day>
<day>
<match>
<team1>foo</team1>
<team2>foo</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<venue>1</venue>
<gmt>10:00</gmt>
<groupe>B</groupe>
</match>
<match>
<team1>foo</team1>
<team2>foo</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<venue>1</venue>
<gmt>14:00</gmt>
<groupe>A</groupe>
</match>
</day>
</data>
<data>
<day>
<match>
<team1>ind</team1>
<team2>afg</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>19.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>eng</team1>
<team2>afg</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>21.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>ind</team1>
<team2>eng</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>23.09</date>
<venue>2</venue>
<gmt>10:00</gmt>
</match>
</day>
<day>
<match>
<team1>aust</team1>
<team2>irl</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>19.08</date>
<venue>1</venue>
<gmt>10:00</gmt>
</match>
<match>
<team1>aust</team1>
<team2>west</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>22.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>west</team1>
<team2>irl</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>24.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
</day>
</data>
尝试使用
DOM解析器
,您将可以完美地访问对象模型树
的任何部分
如果您想对其进行更多控制,请尝试下面提到的XML解析器
-JAXP和JAXB
-
Castor
尝试使用DOM解析器
,您将可以完美地访问对象模型树的任何部分。
如果您想对其进行更多控制,请尝试下面提到的XML解析器
-JAXP和JAXB
-
Castor
一个好的做法是使用反映XML模式的对象模型。类似这样:一个名为Day
的对象,它包含匹配项的集合,其中包含8个字段(team1、team2、result1等等)
使用该模型,解析器所要做的就是在解析过程中构建对象
有了它,您还可以使用一些伟大的框架来将XML文件转换为java对象,比如。一个好的实践是使用反映XML模式的对象模型。类似这样:一个名为Day
的对象,它包含匹配项的集合,其中包含8个字段(team1、team2、result1等等)
使用该模型,解析器所要做的就是在解析过程中构建对象
有了这些,您还可以使用一些伟大的框架来将XML文件转换为java对象,比如。Gilberto关于使对象模型反映XML模式的建议很好
在我对您的域的解释中,我创建了一个类Match
,它或多或少是您的xmlgeterssetters
,一个类Day
,它只包含一个列表
,一个类锦标赛
,它包含一个列表
当然,您可以将整个混乱表示为一个列表
,但是将它们封装在您自己的类中会更好一些
我不得不将您对localName
的所有引用改为使用qName
,这可能只是反映了我测试中的一个错误,但这大致可行,应该可以让您了解如何处理这种结构
上面列出的类非常简单,但它们就在这里
锦标赛
和锦标赛日
只是列表的包装:
public class Tournament {
private List<TournamentDay> days;
public Tournament() {
this.days = new ArrayList<TournamentDay>();
}
public void add(TournamentDay day) {
days.add(day);
}
}
public class TournamentDay {
private List<Match> matches;
public TournamentDay() {
this.matches = new ArrayList<Match>();
}
public void add(Match match) {
matches.add(match);
}
}
构建此结构的处理程序类应该是这样的,使用getTournament()
方法在解析后检索结果
public class TournamentHandler extends DefaultHandler {
private Tournament tournament;
private TournamentDay currentDay;
private Match currentMatch;
private StringBuilder builder;
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("team1"))
currentMatch.setTeam1(builder.toString());
else if (qName.equalsIgnoreCase("team2"))
currentMatch.setTeam2(builder.toString());
else if (qName.equalsIgnoreCase("resultfinal"))
currentMatch.setResultfinal(builder.toString());
else if (qName.equalsIgnoreCase("result1"))
currentMatch.setResult1(builder.toString());
else if (qName.equalsIgnoreCase("result2"))
currentMatch.setResult2(builder.toString());
else if (qName.equalsIgnoreCase("venue"))
currentMatch.setVenue(builder.toString());
else if (qName.equalsIgnoreCase("gmt"))
currentMatch.setGmt(builder.toString());
else if (qName.equals("match"))
currentDay.add(currentMatch);
else if (qName.equals("day"))
tournament.add(currentDay);
}
@Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
if (qName.equals("data")) {
tournament = new Tournament();
}
if (qName.equals("day")) {
currentDay = new TournamentDay();
}
else if (qName.equals("match")) {
currentMatch = new Match();
}
else {
builder = new StringBuilder();
}
}
@Override
public void characters(char[] chars, int start, int length) throws SAXException {
builder.append(chars, start, length);
}
public Tournament getTournament() {
return tournament;
}
}
Gilberto关于让对象模型反映XML模式的建议很好
在我对您的域的解释中,我创建了一个类Match
,它或多或少是您的xmlgeterssetters
,一个类Day
,它只包含一个列表
,一个类锦标赛
,它包含一个列表
当然,您可以将整个混乱表示为一个列表
,但是将它们封装在您自己的类中会更好一些
我不得不将您对localName
的所有引用改为使用qName
,这可能只是反映了我测试中的一个错误,但这大致可行,应该可以让您了解如何处理这种结构
上面列出的类非常简单,但它们就在这里
锦标赛
和锦标赛日
只是列表的包装:
public class Tournament {
private List<TournamentDay> days;
public Tournament() {
this.days = new ArrayList<TournamentDay>();
}
public void add(TournamentDay day) {
days.add(day);
}
}
public class TournamentDay {
private List<Match> matches;
public TournamentDay() {
this.matches = new ArrayList<Match>();
}
public void add(Match match) {
matches.add(match);
}
}
构建此结构的处理程序类应该是这样的,使用getTournament()
方法在解析后检索结果
public class TournamentHandler extends DefaultHandler {
private Tournament tournament;
private TournamentDay currentDay;
private Match currentMatch;
private StringBuilder builder;
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("team1"))
currentMatch.setTeam1(builder.toString());
else if (qName.equalsIgnoreCase("team2"))
currentMatch.setTeam2(builder.toString());
else if (qName.equalsIgnoreCase("resultfinal"))
currentMatch.setResultfinal(builder.toString());
else if (qName.equalsIgnoreCase("result1"))
currentMatch.setResult1(builder.toString());
else if (qName.equalsIgnoreCase("result2"))
currentMatch.setResult2(builder.toString());
else if (qName.equalsIgnoreCase("venue"))
currentMatch.setVenue(builder.toString());
else if (qName.equalsIgnoreCase("gmt"))
currentMatch.setGmt(builder.toString());
else if (qName.equals("match"))
currentDay.add(currentMatch);
else if (qName.equals("day"))
tournament.add(currentDay);
}
@Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
if (qName.equals("data")) {
tournament = new Tournament();
}
if (qName.equals("day")) {
currentDay = new TournamentDay();
}
else if (qName.equals("match")) {
currentMatch = new Match();
}
else {
builder = new StringBuilder();
}
}
@Override
public void characters(char[] chars, int start, int length) throws SAXException {
builder.append(chars, start, length);
}
public Tournament getTournament() {
return tournament;
}
}
处理程序是否也有设置elementValue
的characters
方法?是的,elementValue是通过characters方法设置的?处理程序是否也有设置elementValue
的characters
方法?是,elementValue是由characters方法+1设置的,用于使对象模型反映模式的建议。+1用于使对象模型反映模式的建议。非常感谢,这正是我想要的:)您能解释一下,如何创建这些列表和列表(类比赛和匹配)以及如何检索数据?:)应用程序崩溃时是否提供堆栈跟踪?如果是这样,包括这一点可能会有所帮助。查看活动的代码,我发现您在解析之后创建了一个新的XMLHandler。您需要保留在解析中实际使用的一个,并从中提取结果。非常感谢,这正是我想要的:)您能解释一下,如何创建这些列表和列表(类锦标赛和比赛)以及如何检索数据吗?:)应用程序崩溃时是否提供堆栈跟踪?如果是这样,包括这一点可能会有所帮助。查看活动的代码,我发现您在解析之后创建了一个新的XMLHandler。您需要保留解析中实际使用的方法,并从中提取结果。@raman这是我的建议,但也给出了更简单方法的选项….@raman这是我的建议,但也给出了更简单方法的选项。。。。。