gpt4 book ai didi

android - Android 中的 SAX XML

转载 作者:行者123 更新时间:2023-11-30 03:13:41 25 4
gpt4 key购买 nike

这是我的 XML 文件,我需要根据其名称获取 ID 值。例如,我正在显示主页,*我的结果*,我的出勤 ,,*出勤* ,请假,*请假* ,在 ListView 中申请短期休假

但是,

当我点击主页时,我必须得到2298
的值 当我点击 My Results 时,我必须获得 2431
的值 当我点击 My Attendance 时,我必须获得 2415
的值 当我点击 Home 时,我必须获得 20461 的值 当我点击我的出勤时,我必须像这样获得20613的值

<?xml version="1.0" encoding="utf-8" ?> 
<MenusByRole>


<MenuByRole InstanceID="519" RoleID="614">
<MainMenu Id="2298">Home Page</MainMenu>
<MainMenu Id="2431">My Results</MainMenu>
<MainMenu Id="2415">My Attendance</MainMenu>
<SubMenu Id="6121" MainMenuId="2298">FLASH NEWS</SubMenu>
<SubMenu Id="6122" MainMenuId="2298">BEST PERFORMERS</SubMenu>
<SubMenu Id="6123" MainMenuId="2298">GENERAL NOTICES</SubMenu>
<SubMenu Id="6124" MainMenuId="2298">TIME TABLES And SCHEDULES</SubMenu>
<SubMenu Id="6125" MainMenuId="2298"> COOL LINKS</SubMenu>
<SubMenu Id="6126" MainMenuId="2298">TIP OF THE WEEK</SubMenu>
<SubMenu Id="2416" MainMenuId="2415">Attendance Details</SubMenu>
<SubMenu Id="2417" MainMenuId="2415">Summary</SubMenu>
<SubMenu Id="14678" MainMenuId="2431">Previous Result</SubMenu>
</MenuByRole>

<MenuByRole InstanceID="604" RoleID="1184">
<MainMenu Id="20461">Home</MainMenu>
<MainMenu Id="20613">Attendance</MainMenu>
<MainMenu Id="20933">Apply Leave</MainMenu>
<MainMenu Id="22856">Apply Student Leave</MainMenu>
<MainMenu Id="22855">Apply Short Leave</MainMenu>
<SubMenu Id="20616" MainMenuId="20613" Url="/">Post Attendance</SubMenu>
<SubMenu Id="6121" MainMenuId="20461" Url="/Notices.xaml">FLASH NEWS</SubMenu>
<SubMenu Id="6122" MainMenuId="20461" Url="/Notices.xaml">BEST PERFORMERS</SubMenu>
<SubMenu Id="6123" MainMenuId="20461" Url="/Notices.xaml">GENERAL NOTICES</SubMenu>
<SubMenu Id="6131" MainMenuId="20461" Url="/Notices.xaml">TIME TABLES</SubMenu>
<SubMenu Id="6125" MainMenuId="20461" Url="/Notices.xaml">COOL LINKS</SubMenu>
<SubMenu Id="6137" MainMenuId="20461" Url="/Notices.xaml">THOUGHT OF THE DAY</SubMenu>
</MenuByRole>

</MenusByRole>

这是我用过的代码...

public class MainMenu {
private String id;
private String Name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

}

MainActivity.java 页面

public class MainActivity extends Activity {
static String id, instanceid, roleid, subid, s, iid, rid, userid;
ArrayList<String> al = new ArrayList<String>();

ArrayList<MainMenu> munuIDList;
MainMenu oMainMenu;
boolean isReadData;
String sName;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ListView lv = (ListView) findViewById(R.id.listView1);

try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {


public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {

if (qName.equalsIgnoreCase("MenuByRole")) {
iid = attributes.getValue("InstanceID");
rid = attributes.getValue("RoleID");

munuIDList = new ArrayList<MainMenu>();
isReadData = false;
}
if (qName.equalsIgnoreCase("MainMenu")) {
id = attributes.getValue("Id");

isReadData = true;
oMainMenu = new MainMenu();
oMainMenu.setId(attributes.getValue("Id"));
}



}

public void endElement(String uri, String localName,
String qName) throws SAXException {
if (localName.equalsIgnoreCase("MainMenu")) {

oMainMenu.setName(sName);
munuIDList.add(oMainMenu);
al.add(oMainMenu.getName());

}
sName = null;
isReadData = false;
}

public void characters(char ch[], int start, int length)
throws SAXException {

if (isReadData) {
sName = new String(ch, start, length);
}

}
};
InputStream is = getAssets().open("menus.xml");
saxParser.parse(is, handler);

} catch (Exception e) {
e.printStackTrace();
}
lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.select_dialog_item, al));
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SubMenu.alnew.clear();
s = munuIDList.get(arg2).getId();
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_SHORT).show();
}
});
}



}

这里当我点击主页时,它应该在我的Toast消息中显示2298作为输出

但它显示 20461 作为输出

我无法解决这个问题。请帮我解决这个 Parsing 问题。

最佳答案

删除 ArrayList 的首字母 munuIDList = new ArrayList();

if (qName.equalsIgnoreCase("MenuByRole")) {
iid = attributes.getValue("InstanceID");
rid = attributes.getValue("RoleID");

//munuIDList = new ArrayList<MainMenu>();
isReadData = false;
}

关于android - Android 中的 SAX XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512400/

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