gpt4 book ai didi

java - 使用 SAX 解析器获取特定子节点

转载 作者:行者123 更新时间:2023-12-01 21:29:24 25 4
gpt4 key购买 nike

强调文本我有以下 xml:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Game Analysis</title>
<item>
<title>Game</title>
<description>ABC</description>
<releaseDate>Sat, 21 Feb 2012 05:18:23 GMT</releaseDate>
</item>
<item>
<title>CoD</title>
<description>XYZ</description>
<releaseDate>Sat, 21 Feb 2011 05:18:23 GMT</releaseDate>
</item>
</channel>
</rss>

我必须解析这个 xml 并获取“item”下的所有子节点,然后检查它是否包含“releaseDate”节点。如果不是,那么我必须抛出异常。

我也尝试过使用 xpath,但它不起作用。

    XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//channel/item");

Object result = expr.evaluate(document, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getChildNodes());
}

最佳答案

试试这个代码。不要忘记在您的项目中包含 SAX 解析器库并从 XML 文档中删除 rss 字符串(希望这被接受)。

public class SaxParserTest {
public static void main(String... argv) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
MyHandler handler = new MyHandler();
saxParser.parse(new File("your path to XML-file here"), handler);
List<Item> items = handler.getChannel().getItems();
// your check of item release dates here
} catch (Exception e) {
e.printStackTrace();
}
}
}

class MyHandler extends DefaultHandler {
private StringBuilder data = new StringBuilder();

private Channel channel;

private String itemTitle;
private String itemDescription;
private String itemReleaseDate;

private boolean isItem;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (!qName.equals("rss")) {
if (qName.equalsIgnoreCase("channel")) {
channel = new Channel();
} else if (qName.equalsIgnoreCase("item")) {
isItem = true;
}
data.setLength(0);
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("title")) {
if (!isItem) {
channel.setTitle(data.toString());
} else {
itemTitle = data.toString();
}
} else if (qName.equalsIgnoreCase("item")) {
channel.addItem(new Item(itemTitle, itemDescription, itemReleaseDate));
itemTitle = null;
itemDescription = null;
itemReleaseDate = null;
isItem = false;
} else if (qName.equalsIgnoreCase("description")) {
itemDescription = data.toString();
} else if (qName.equalsIgnoreCase("releaseDate")) {
itemReleaseDate = data.toString();
}
}

@Override
public void characters(char ch[], int start, int length) throws SAXException {
data.append(new String(ch, start, length));
}

public Channel getChannel() {
return channel;
}
}

class Channel {
private String title;
private List<Item> items;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public List<Item> getItems() {
return items;
}

public void setItems(List<Item> items) {
this.items = items;
}

public void addItem(Item item) {
if (items == null) {
items = new ArrayList<Item>();
}
items.add(item);
}
}

class Item {
private String title;
private String description;
private String releaseDate;

public Item(String title, String description, String releaseDate) {
this.title = title;
this.description = description;
this.releaseDate = releaseDate;
}
public String getReleaseDate() {
return releaseDate;
}
}

关于java - 使用 SAX 解析器获取特定子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58817358/

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