gpt4 book ai didi

java - stax从元素中获取属性值

转载 作者:行者123 更新时间:2023-11-30 09:42:48 27 4
gpt4 key购买 nike

我尝试使用 STAX 从 URI 解析 XML 文件!

XML 看起来像这样:

   <Type type_id="4218">
<Title>English Premier League</Title>

<Event start_time="2011-12-18 16:10:00" ev_id="2893772">
<Description>Manchester City v Arsenal</Description>

<Market mkt_typ="Win/Draw/Win">
<Occurrence bet_id="42455761" decimal="1.6666666666667">
<Description>Manchester City</Description>
</Occurrence>
<Occurrence bet_id="42455762" decimal="3.6">
<Description>Draw</Description>
</Occurrence>
<Occurrence bet_id="42455764" decimal="5">
<Description>Arsenal</Description>
</Occurrence>
</Market>

</Event>
</Type>

输出应该是:

id:4218
title:English Premier League
ev_id:2893772
date of match:Sun Dec 18 16:10:00 CET 2011
description:Manchester City v Arsenal
one:1.6666666666667
draw:3.6
two:5

我的代码看起来像这样:

XMLInputFactory factory = XMLInputFactory.newInstance();

try {
XMLStreamReader streamReader = factory.createXMLStreamReader(new URL("http://cubs.bluesq.com/cubs/cubs.php?action=getpage&thepage=385.xml").openStream());


while (streamReader.hasNext()) {
int event = streamReader.next();

if(event == XMLStreamConstants.START_ELEMENT){
if(streamReader.getLocalName().equals("Type")){
long id = Integer.parseInt(streamReader.getAttributeValue(null, "type_id"));
if( id == 4218){
System.out.println("id:"+id);
streamReader.nextTag();
System.out.println("title:"+streamReader.getElementText());
streamReader.nextTag();
int ev_id = Integer.parseInt(streamReader.getAttributeValue(null, "ev_id"));
System.out.println("ev_id:"+ev_id);
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
formater.setTimeZone(TimeZone.getTimeZone("CET"));
String tempDatum = streamReader.getAttributeValue(null,"start_time");
Date dateOfMatch = formater.parse(tempDatum);

System.out.println("date of match:"+dateOfMatch);
streamReader.nextTag();
String description = streamReader.getElementText();

System.out.println("description:"+ description);
streamReader.nextTag();
String market = streamReader.getAttributeValue(null, "mkt_typ");
if(market.equals("Win/Draw/Win")){
streamReader.nextTag();
double one = Double.parseDouble(streamReader.getAttributeValue(null, "decimal"));
System.out.println("one:"+ one);

double draw = Double.parseDouble(streamReader.getAttributeValue(null, "decimal"));
System.out.println("draw:"+draw);
double two = Double.parseDouble(streamReader.getAttributeValue(null, "decimal"));
System.out.println("two:"+two);

}


}
}
}

}

此代码产生输出:

id:4218
title:English Premier League
ev_id:2893772
date of match:Sun Dec 18 16:10:00 CET 2011
description:Manchester City v Arsenal
one:1.6666666666667
draw:1.6666666666667
two:1.6666666666667

如何从元素“Occurrence”中的属性“decimal”获取其他值???

最佳答案

if(market.equals("Win/Draw/Win")){
streamReader.nextTag();

我只是快速浏览了一下,但我想说您可能停留在第一个 Occurence 元素处,因为您只调用了一次 streamReader.nextTag(),但调用了 streamReader.getAttributeValue 三次。

关于java - stax从元素中获取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8509984/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com