JAVA 使用 stax 解析器从 xml 获取属性值
JAVA get attributes value from xml using stax parser
我正在开发一个使用 java 读取 RSS 提要的项目,我使用他们使用 Stax 的本教程 parser.My 问题是我如何读取属性值?
http://www.vogella.com/tutorials/RSSFeed/article.html
这是 RSSReader class,
package de.vogella.rss.read;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.XMLEvent;
import de.vogella.rss.model.Feed;
import de.vogella.rss.model.FeedMessage;
public class RSSFeedParser {
static final String TITLE = "title";
static final String DESCRIPTION = "description";
static final String CHANNEL = "channel";
static final String LANGUAGE = "language";
static final String COPYRIGHT = "copyright";
static final String LINK = "link";
static final String AUTHOR = "author";
static final String ITEM = "item";
static final String PUB_DATE = "pubDate";
static final String GUID = "guid";
final URL url;
public RSSFeedParser(String feedUrl) {
try {
this.url = new URL(feedUrl);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public Feed readFeed() {
Feed feed = null;
try {
boolean isFeedHeader = true;
// Set header values intial to the empty string
String description = "";
String title = "";
String link = "";
String language = "";
String copyright = "";
String author = "";
String pubdate = "";
String guid = "";
// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Setup a new eventReader
InputStream in = read();
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// read the XML document
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
String localPart = event.asStartElement().getName().getLocalPart();
switch (localPart) {
case ITEM:
if (isFeedHeader) {
isFeedHeader = false;
feed = new Feed(title, link, description, language,copyright, pubdate);
}
event = eventReader.nextEvent();
break;
case TITLE:
title = getCharacterData(event, eventReader);
break;
case DESCRIPTION:
description = getCharacterData(event, eventReader);
break;
case LINK:
link = getCharacterData(event, eventReader);
break;
case GUID:
guid = getCharacterData(event, eventReader);
break;
case LANGUAGE:
language = getCharacterData(event, eventReader);
break;
case AUTHOR:
author = getCharacterData(event, eventReader);
break;
case PUB_DATE:
pubdate = getCharacterData(event, eventReader);
break;
case COPYRIGHT:
copyright = getCharacterData(event, eventReader);
break;
}
} else if (event.isEndElement()) {
if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
FeedMessage message = new FeedMessage();
message.setAuthor(author);
message.setDescription(description);
message.setGuid(guid);
message.setLink(link);
message.setTitle(title);
feed.getMessages().add(message);
event = eventReader.nextEvent();
continue;
}
}
}
}catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return feed;
}
private String getCharacterData(XMLEvent event, XMLEventReader eventReader)throws XMLStreamException {
String result = "";
event = eventReader.nextEvent();
if (event instanceof Characters) {
result = event.asCharacters().getData();
}
return result;
}
private InputStream read() {
try {
return url.openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
4.2。测试代码
下面使用main方法进行测试。您也可以使用 JUnit。
package de.vogella.rss.tests;
import de.vogella.rss.model.Feed;
import de.vogella.rss.model.FeedMessage;
import de.vogella.rss.read.RSSFeedParser;
public class ReadTest {
public static void main(String[] args) {
RSSFeedParser parser = new RSSFeedParser("http://www.vogella.com/article.rss");
Feed feed = parser.readFeed();
System.out.println(feed);
for (FeedMessage message : feed.getMessages()) {
System.out.println(message);
}
}
}
项目class
package de.vogella.xml.stax.model;
public class Item {
private String date;
private String mode;
private String unit;
private String current;
private String interactive;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getMode() {
return mode;
}
public void setMode(String mode) {
this.mode = mode;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getCurrent() {
return current;
}
public void setCurrent(String current) {
this.current = current;
}
public String getInteractive() {
return interactive;
}
public void setInteractive(String interactive) {
this.interactive = interactive;
}
@Override
public String toString() {
return "Item [current=" + current + ", date=" + date + ", interactive="+ interactive + ", mode=" + mode + ", unit=" + unit + "]";
}
}
feedMessage
package de.vogella.rss.model;
/*
* Represents one RSS message
*/
public class FeedMessage {
String title;
String description;
String link;
String author;
String guid;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
@Override
public String toString() {
return "FeedMessage [title=" + title + ", description=" + description + ", link=" + link + ", author=" + author + ", guid=" + guid + "]";
}
}
我在多次尝试并查看 RSSFEEDPARSER class.
上的 java api 文档后找到了答案
我添加了这些行;我的目标是在缩略图标签中找到属性 'url',因此,在切换之前或添加之后,
// get attribute of thumbnail Tag
//get thumbnail ppicture link
if(localPart.equals("thumbnail")){
Iterator<Attribute> attribue = event.asStartElement().getAttributes();
while(attribue.hasNext()){
Attribute myAttribute = attribue.next();
if(myAttribute.getName().toString().equals("url")){
thumbnail = myAttribute.getValue();
}
}
}
我正在开发一个使用 java 读取 RSS 提要的项目,我使用他们使用 Stax 的本教程 parser.My 问题是我如何读取属性值?
http://www.vogella.com/tutorials/RSSFeed/article.html
这是 RSSReader class,
package de.vogella.rss.read;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.XMLEvent;
import de.vogella.rss.model.Feed;
import de.vogella.rss.model.FeedMessage;
public class RSSFeedParser {
static final String TITLE = "title";
static final String DESCRIPTION = "description";
static final String CHANNEL = "channel";
static final String LANGUAGE = "language";
static final String COPYRIGHT = "copyright";
static final String LINK = "link";
static final String AUTHOR = "author";
static final String ITEM = "item";
static final String PUB_DATE = "pubDate";
static final String GUID = "guid";
final URL url;
public RSSFeedParser(String feedUrl) {
try {
this.url = new URL(feedUrl);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public Feed readFeed() {
Feed feed = null;
try {
boolean isFeedHeader = true;
// Set header values intial to the empty string
String description = "";
String title = "";
String link = "";
String language = "";
String copyright = "";
String author = "";
String pubdate = "";
String guid = "";
// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Setup a new eventReader
InputStream in = read();
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// read the XML document
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
String localPart = event.asStartElement().getName().getLocalPart();
switch (localPart) {
case ITEM:
if (isFeedHeader) {
isFeedHeader = false;
feed = new Feed(title, link, description, language,copyright, pubdate);
}
event = eventReader.nextEvent();
break;
case TITLE:
title = getCharacterData(event, eventReader);
break;
case DESCRIPTION:
description = getCharacterData(event, eventReader);
break;
case LINK:
link = getCharacterData(event, eventReader);
break;
case GUID:
guid = getCharacterData(event, eventReader);
break;
case LANGUAGE:
language = getCharacterData(event, eventReader);
break;
case AUTHOR:
author = getCharacterData(event, eventReader);
break;
case PUB_DATE:
pubdate = getCharacterData(event, eventReader);
break;
case COPYRIGHT:
copyright = getCharacterData(event, eventReader);
break;
}
} else if (event.isEndElement()) {
if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
FeedMessage message = new FeedMessage();
message.setAuthor(author);
message.setDescription(description);
message.setGuid(guid);
message.setLink(link);
message.setTitle(title);
feed.getMessages().add(message);
event = eventReader.nextEvent();
continue;
}
}
}
}catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return feed;
}
private String getCharacterData(XMLEvent event, XMLEventReader eventReader)throws XMLStreamException {
String result = "";
event = eventReader.nextEvent();
if (event instanceof Characters) {
result = event.asCharacters().getData();
}
return result;
}
private InputStream read() {
try {
return url.openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
4.2。测试代码
下面使用main方法进行测试。您也可以使用 JUnit。
package de.vogella.rss.tests;
import de.vogella.rss.model.Feed;
import de.vogella.rss.model.FeedMessage;
import de.vogella.rss.read.RSSFeedParser;
public class ReadTest {
public static void main(String[] args) {
RSSFeedParser parser = new RSSFeedParser("http://www.vogella.com/article.rss");
Feed feed = parser.readFeed();
System.out.println(feed);
for (FeedMessage message : feed.getMessages()) {
System.out.println(message);
}
}
}
项目class
package de.vogella.xml.stax.model;
public class Item {
private String date;
private String mode;
private String unit;
private String current;
private String interactive;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getMode() {
return mode;
}
public void setMode(String mode) {
this.mode = mode;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getCurrent() {
return current;
}
public void setCurrent(String current) {
this.current = current;
}
public String getInteractive() {
return interactive;
}
public void setInteractive(String interactive) {
this.interactive = interactive;
}
@Override
public String toString() {
return "Item [current=" + current + ", date=" + date + ", interactive="+ interactive + ", mode=" + mode + ", unit=" + unit + "]";
}
}
feedMessage
package de.vogella.rss.model;
/*
* Represents one RSS message
*/
public class FeedMessage {
String title;
String description;
String link;
String author;
String guid;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
@Override
public String toString() {
return "FeedMessage [title=" + title + ", description=" + description + ", link=" + link + ", author=" + author + ", guid=" + guid + "]";
}
}
我在多次尝试并查看 RSSFEEDPARSER class.
上的 java api 文档后找到了答案我添加了这些行;我的目标是在缩略图标签中找到属性 'url',因此,在切换之前或添加之后,
// get attribute of thumbnail Tag
//get thumbnail ppicture link
if(localPart.equals("thumbnail")){
Iterator<Attribute> attribue = event.asStartElement().getAttributes();
while(attribue.hasNext()){
Attribute myAttribute = attribue.next();
if(myAttribute.getName().toString().equals("url")){
thumbnail = myAttribute.getValue();
}
}
}