嵌套 XML 拉解析器问题,无法获取子元素

Nested XML pull parser issue ,cannot get child elements

我无法从该 XML 中获取子元素,我正在获取作业值,但无法访问该任务并将它们与各自的作业分开。我真的需要帮助,下面是我尝试过的解决方案,无法将任务数组嵌套到父数组。

 <root>
    <job>
      <id>3</id>
      <job_title/>
      <job_description/>
      <job_room>abx.jpg</job_room>
      <status/>
       <task>no task</task>
    </job>
    <job>
      <id>2</id>
      <job_title/>
      <job_description/>
      <job_room>ddemo.jpg</job_room>
      <status/>
      <task>
        <task_id/>
        <task_title/>
        <task_description/>
        <task_status>0</task_status>
        </task>
    </job>
    <job>
      <id>60</id>
      <job_title>CTO</job_title>
      <job_description>CTO</job_description>
      <job_room>4564</job_room>
      <status>open</status>
      <task>
         <task_id>1</task_id>
         <task_title>Add crib</task_title>
         <task_description/>
         <task_status>0</task_status>
      </task>
      <task>
        <task_id>2</task_id>
        <task_title/>
        <task_description/>
        <task_status>0</task_status>
      </task>
    </job>
 </root>

,这是我试过的解决方案。

public static List<Job> parseFeed(String content) {

        try {

            boolean inDataItemTag = false;
            String currentTagName = "";
            Job job = null;
            List<Job> jobList = new ArrayList<>();

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(new StringReader(content));

            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {

                switch (eventType) {
                case XmlPullParser.START_TAG:
                    currentTagName = parser.getName();
                    if (currentTagName.equals("job")) {
                        inDataItemTag = true;
                        job = new Job();
                        jobList.add(job);
                    }
                    break;

                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("job")) {
                        inDataItemTag = false;
                    }
                    currentTagName = "";
                    break;

                case XmlPullParser.TEXT:
                    if (inDataItemTag && job != null) {
                        switch (currentTagName) {
                        case "id":
                            job.setId(parser.getText());
                            System.out.println(job.getId());
                            break;
                        case "job_description":
                            job.setJob_description(parser.getText());
                            System.out.println(job.getJob_description());
                            break;
                        case "job_room":
                            job.setJob_room(parser.getText());
                            break;
                        case "job_title":
                            job.setJob_title(parser.getText());
                            System.out.println("JOB TITLE");
                            break;
                        case "status" :
                            job.setStatus(parser.getText());
                            break;
                        case "task" :
                            job.setChildren(getTask(content));  
                        default:
                            break;
                        }
                    }
                    break;
                }

                eventType = parser.next();

            }

            return jobList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } 

    }

    public static List<Task> getTask(String content) {

        try {

            boolean inDataItemTag = false;
            String currentTagName = "";
            Task task = null;
            List<Task> taskList = new ArrayList<>();

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(new StringReader(content));

            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT ) {

                switch (eventType) {
                case XmlPullParser.START_TAG:
                    currentTagName = parser.getName();
                    if (currentTagName.equals("task")) {
                        inDataItemTag = true;
                        task = new Task();
                        taskList.add(task);
                    }
                    break;

                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("task")) {
                        inDataItemTag = false;
                    }
                    currentTagName = "";
                    break;

                case XmlPullParser.TEXT:
                    if (inDataItemTag && task != null) {
                        switch (currentTagName) {
                        case "task_id":
                            task.setTask_id(parser.getText());
                            System.out.println(task.getTask_id());
                            break;
                        case "task_description":
                            task.setTask_description(parser.getText());
                            System.out.println(task.getTask_description());
                            break;
                        case "task_title":
                            task.setTask_title(parser.getText());
                            break;
                        case "task_status" :
                            task.setTask_status(parser.getText());
                            break;
                        default:
                            break;
                        }
                    }
                    break;
                }


                if(currentTagName.equals("job"))
                {
                    break;
                }

                eventType = parser.next();


            }

            return taskList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } 

    }

这是我的物品

public class Job {

    private String id;
    private String job_title;
    private String job_description;
    private String job_room;
    private String status;

    private List<Task> children;

    public String getId() {
        return id;
    }

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

    public String getJob_title() {
        return job_title;
    }

    public void setJob_title(String job_title) {
        this.job_title = job_title;
    }

    public String getJob_description() {
        return job_description;
    }

    public void setJob_description(String job_description) {
        this.job_description = job_description;
    }

    public String getJob_room() {
        return job_room;
    }

    public void setJob_room(String job_room) {
        this.job_room = job_room;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<Task> getChildren() {
        return children;
    }

    public void setChildren(List<Task> children) {
        this.children = children;
    }
}

public class Task {

    private String task_id;
    private String task_title;
    private String task_description;
    private String task_status;
    public String getTask_id() {
        return task_id;
    }
    public void setTask_id(String task_id) {
        this.task_id = task_id;
    }
    public String getTask_title() {
        return task_title;
    }
    public void setTask_title(String task_title) {
        this.task_title = task_title;
    }
    public String getTask_description() {
        return task_description;
    }
    public void setTask_description(String task_description) {
        this.task_description = task_description;
    }
    public String getTask_status() {
        return task_status;
    }
    public void setTask_status(String task_status) {
        this.task_status = task_status;
    }
}

我可以建议使用 JAXB 来完成这个任务,试试这个

public class Test { 
    @XmlElement
    List<Job> job;

    public static void main(String[] args) throws Exception {
        List<Job> jobs = JAXB.unmarshal(new File("1.xml"), Test.class).job;
    }
}

这是一个使用 data projection:

的完整示例(包括您的模型)
public class ReadJobs {

    public interface Projection extends Serializable {
        interface Job {
            @XBRead("./task")
            List<Task> getTasks();

            @XBRead("./id")
            String getID();
        }

        interface Task {
            @XBRead("./task_id")
            String getID();
        }

        @XBRead("/root/job")
        List<Job> getJobs();
    }

    public static void main(String... args) throws IOException {
        Projection projection = new XBProjector().io().url("res://jobs.xml").read(Projection.class);
        for (Job job : projection.getJobs()) {
            System.out.println(job.getID());
            for (Task task : job.getTasks()) {
                System.out.println(task.getID());
            }
        }
    }
}

修复 xml(添加结束根标记)时,此程序打印出:

3
null
2

60
1
2