我想从服务器创建一个android应用程序获取xml数据并将XML数据解析为object。 我想将下面的数据解析为5 List:
private List<TaskScheduleDto> TaskShedule;
private List<ContractOptionInListDto> ContractOptionInList;
private List<ContractOptionOutListDto> ContractOptionOutList;
private List<OutsourceListDto> OutsourceList;
private List<CheckListDto> CheckList;
这是我的XML数据。
<?xml version="1.0" encoding="utf-8"?>
<Response>
<ReturnCode>0</ReturnCode>
<Message></Message>
<Result>
<Task>
<TaskShedule>
<building_id>1</building_id>
<room_id>12</room_id>
<building_name>Gacon</building_name>
<room_name>Lala</room_name>
<management_area_name>Heocon</management_area_name>
</TaskShedule>
<ContractOptionInList>
<ContractOption>
<task_shedule_id>2</task_shedule_id>
<contract_option_id>2</contract_option_id>
<task_kbn>2</task_kbn>
<option_name>2</option_name>
<status>1</status>
</ContractOption>
</ContractOptionInList>
<ContractOptionOutList>
<ContractOption>
<task_shedule_id>2</task_shedule_id>
<contract_option_id>2</contract_option_id>
<task_kbn>2</task_kbn>
<option_name>2</option_name>
<status>1</status>
</ContractOption>
</ContractOptionOutList>
<Outsourece>
<task_shedule_id>2</task_shedule_id>
<outsource_id>2</outsource_id>
<purpose>2</purpose>
<memo>2</memo>
<fixed_date>1</fixed_date>
<date_type>2</date_type>
<contract_option_id>2</contract_option_id>
<variable_count>2</variable_count>
<status>2</status>
</Outsourece>
<CheckList>
<task_shedule_id></task_shedule_id>
<Group>
<code>123</code>
<name>Heoheo</name>
<CheckItem>
<check_item>Lalala</check_item>
<check_detail>9987</check_detail>
<status>1</status>
<code>011</code>
</CheckItem>
</Group>
</CheckList>
</Task>
</Result>
</Response>
我怎么能这样做? 任何帮助非常感谢!感谢。
答案 0 :(得分:0)
尝试这个,按照您的要求完成,但添加更多来完成此操作。希望这对你有帮助,也为每个类添加setter方法。,bcz我在这里使用setter方法。
例如:
public void setName(String name){
this.name = name;
}
import java.io.FileInputStream;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.provider.Telephony.Mms.Addr;
import android.util.Log;
public class MyModeXMLParser {
private List<TaskScheduleDto> TaskShedule;
private List<ContractOptionInListDto> ContractOptionInList;
private List<ContractOptionOutListDto> ContractOptionOutList;
private List<OutsourceListDto> OutsourceList;
private List<CheckListDto> CheckList;
public void parse(FileInputStream is) {
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
// create the objects for classes when getting a class name as open tag
if (tagname.equalsIgnoreCase("TaskShedule"))
{ taskScheduleDto = new TaskScheduleDto();
}
else if (tagname.equalsIgnoreCase("ContractOptionInList"))
{ contractOptionInListDto = new ContractOptionInListDto();
}
else if (tagname.equalsIgnoreCase("ContractOptionOutList"))
{ contractOptionOutListDto = new ContractOptionOutListDto();
}
else if (tagname.equalsIgnoreCase("Outsourece"))
{ outsourceListDto = new OutsourceListDto();
}
else if (tagname.equalsIgnoreCase("CheckList"))
{ checkListDto = new CheckListDto();
}
break;
case XmlPullParser.TEXT:
// GETTING TEXT ENCLOSED BY ANY TAG
text = parser.getText();
break;
case XmlPullParser.END_TAG: if (tagname.equalsIgnoreCase("TaskShedule"))
{
// ADD THE OBJECT TO LIST WHILE GETTING THE END TAG
TaskShedule.add(taskScheduleDto);
}
// SET THE VALUE OF EACH MEMBERS OF THE CLASS
else if (tagname.equalsIgnoreCase("building_id")) {
taskScheduleDto.setBuildingID(text);
}
else if (tagname.equalsIgnoreCase("room_id>")) {
taskScheduleDto.setRoomID(text);
}
else if (tagname.equalsIgnoreCase("building_name")) {
taskScheduleDto.setBuildingName(text);
}
else if (tagname.equalsIgnoreCase("room_name")) {
taskScheduleDto.setRoomName(text);
}
else if (tagname.equalsIgnoreCase("management_area_name")) {
taskScheduleDto.setManagmentAreaName(text);
}
else if (tagname.equalsIgnoreCase("ContractOptionInList"))
{ ContractOptionInList.add(contractOptionInListDto);
}
// SET THE VALUE OF EACH MEMBERS OF THE CLASS
// replace it with your required!
// else if (tagname.equalsIgnoreCase("building_id")) {
// taskScheduleDto.setBuildingID(text);
// }
// else if (tagname.equalsIgnoreCase("room_id>")) {
// taskScheduleDto.setRoomID(text);
// }
// else if (tagname.equalsIgnoreCase("building_name")) {
// taskScheduleDto.setBuildingName(text);
// }
// else if (tagname.equalsIgnoreCase("room_name")) {
// taskScheduleDto.setRoomName(text);
// }
// else if (tagname.equalsIgnoreCase("management_area_name")) {
// taskScheduleDto.setManagmentAreaName(text);
// }
else if (tagname.equalsIgnoreCase("ContractOptionOutList"))
{ ContractOptionOutList.add(contractOptionOutListDto);
}
// SET THE VALUE OF EACH MEMBERS OF THE CLASS
// replace it with your required!
// else if (tagname.equalsIgnoreCase("building_id")) {
// taskScheduleDto.setBuildingID(text);
// }
// else if (tagname.equalsIgnoreCase("room_id>")) {
// taskScheduleDto.setRoomID(text);
// }
// else if (tagname.equalsIgnoreCase("building_name")) {
// taskScheduleDto.setBuildingName(text);
// }
// else if (tagname.equalsIgnoreCase("room_name")) {
// taskScheduleDto.setRoomName(text);
// }
// else if (tagname.equalsIgnoreCase("management_area_name")) {
// taskScheduleDto.setManagmentAreaName(text);
// }
else if (tagname.equalsIgnoreCase("Outsourece"))
{ OutsourceList.add(outsourceListDto);
}
// SET THE VALUE OF EACH MEMBERS OF THE CLASS
// replace it with your required!
// else if (tagname.equalsIgnoreCase("building_id")) {
// taskScheduleDto.setBuildingID(text);
// }
// else if (tagname.equalsIgnoreCase("room_id>")) {
// taskScheduleDto.setRoomID(text);
// }
// else if (tagname.equalsIgnoreCase("building_name")) {
// taskScheduleDto.setBuildingName(text);
// }
// else if (tagname.equalsIgnoreCase("room_name")) {
// taskScheduleDto.setRoomName(text);
// }
// else if (tagname.equalsIgnoreCase("management_area_name")) {
// taskScheduleDto.setManagmentAreaName(text);
// }
else if (tagname.equalsIgnoreCase("CheckList"))
{ CheckList.add(checkListDto);
// SET THE VALUE OF EACH MEMBERS OF THE CLASS
// replace it with your required!
// else if (tagname.equalsIgnoreCase("building_id")) {
// taskScheduleDto.setBuildingID(text);
// }
// else if (tagname.equalsIgnoreCase("room_id>")) {
// taskScheduleDto.setRoomID(text);
// }
// else if (tagname.equalsIgnoreCase("building_name")) {
// taskScheduleDto.setBuildingName(text);
// }
// else if (tagname.equalsIgnoreCase("room_name")) {
// taskScheduleDto.setRoomName(text);
// }
// else if (tagname.equalsIgnoreCase("management_area_name")) {
// taskScheduleDto.setManagmentAreaName(text);
// }
} break;
default: break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
您可以在此处参考XML Pullparser和示例代码:http://theopentutorials.com/tutorials/android/xml/android-simple-xmlpullparser-tutorial/
答案 1 :(得分:0)
请尝试https://gitlab.com/mvysny/konsume-xml。它也是基于StAX的,但是引入了更易于使用的高级API。
代码如下:
const yargs = require("yargs")
yargs.command({
command: 'add',
describe: 'ADD A NOTE TO A FILE',
handler: function(){
console.log('Adding to a file')
}
})
这将打印root# node mainFile_node.js --help
。这种方法的优点是konsume-xml还将检查XML的有效性-任何未知标签都将通过有意义的错误消息使解析失败。
免责声明:我是作者。