gpt4 book ai didi

java - android sax xml 解析多个父级

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

我正在尝试通过sax解析技术从xml页面解析数据。xml页面的结构是

<?xml version="1.0" encoding="UTF-8"?>
<news_magazine>
<latest_news>
<news>
<category_id>5</category_id>
<author>Super Admin</author>
</news>
<news>
<category_id>6</category_id>
<author>user</author>
</news>
</latest_news>
<categories>
<category>
<category_id>5</category_id>
<category_name>cricket</category_name>
</category>
<category>
<category_id>5</category_id>
<category_name>cricket</category_name>
</category>
</categories>
</news_magazine>

新闻标签和分类标签都包含分类id,如何分别获取分类id?

最佳答案

嘿,你可以用非常简单的方式做到这一点。只需维护一个额外的变量当您在解析器中放置条件时,当您获得任何“新闻”或“类别”标记时,而不是在 current_parent 中分配该标记。并且在比较标签“category_id”时还添加了 current_parent 的条件

public static String current_parent;
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
if (localName.equals("news") {
current_parent = "news";
// Do your code with news.
return;
}
else if (localName.equals("category") {
current_parent = "category";
// Do your code with category.
return;
}
else if (localName.equals("category_id") {
if(current_parent.equals("category")
{
// Do your code with category's category_id.
}else if(current_parent.equals("news")
{
// Do your code with news's category_id.
}
return;
}
}

关于java - android sax xml 解析多个父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835906/

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