如何解析String xml以使用Java返回所有遍历路径

时间:2014-08-21 12:30:57

标签: java xml xml-parsing sax stax

我正在尝试解析xml String以返回从root到depth的所有遍历路径。 请帮助满足我的要求。

我的xml字符串如下:

<levels>
    <level>
        <id>123</id>
        <name>Leave Review by Manager</name>
        <task>Review by Manager</task>
        <clauses>
            <clause>
                <id>123</id>
                <name>Leave Review By Manager</name>
                <role>123</role>
                <users>
                    <user>
                        <id>abcd@xyz.com</id>
                        <name>ABC D</name>
                    </user>
                </users>
                <actions>
                    <action>
                        <id>123</id>
                        <name>Approved</name>
                        <type>Accepted</type>
                        <caption>Approve</caption>
                        <level />
                    </action>
                    <action>
                        <id>123</id>
                        <name>Rejected by Manager</name>
                        <type>Rejected</type>
                        <caption>Reject</caption>
                        <level />
                    </action>
                </actions>
            </clause>
        </clauses>
    </level>
</levels>

预期输出:以HashMap或列表类型

的形式返回所有遍历路径
levelId="123",  levelName="Leave Review by Manager",    taskName="Review by Manager",   clauseId="123", clauseName="Leave Review By Manager", clauseRole="123", userId="abc.d@xyz.com", userName="ABC D"    actionId="123", actionName="Approved", actionType="Accepted", actionCaption="Approve"

levelId="123",  levelName="Leave Review by Manager",    taskName="Review by Manager",   clauseId="123", clauseName="Leave Review By Manager", clauseRole="123", userId="abc.d@xyz.com", userName="ABC D"    actionId="123", actionName="Rejected by Manager", actionType="Rejected", actionCaption="Reject"

我用Google搜索并进行了以下尝试,但没有得到预期的上述输出:

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    List<String> list = new ArrayList<String>();
    String levelName = "";
    String actionType = "";
    String actionCaption = "";
    String userId = "";
    String actionName = "";

    try {
        XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName));
        while(xmlEventReader.hasNext()){
            XMLEvent xmlEvent = xmlEventReader.nextEvent();
           if (xmlEvent.isStartElement()){
               StartElement startElement = xmlEvent.asStartElement();
               if(startElement.getName().getLocalPart().equals("level")){

                   //Get the 'levelName' attribute from level element
                   Attribute idAttr = startElement.getAttributeByName(new QName("name"));
                   if(idAttr != null){
                 levelName = xmlEvent.asCharacters().getData();
                   }
               }
               //set the other varibles from xml elements
               else if(startElement.getName().getLocalPart().equals("type")){
                   xmlEvent = xmlEventReader.nextEvent();
                 actionType = (xmlEvent.asCharacters().getData());
               }else if(startElement.getName().getLocalPart().equals("name")){
                   xmlEvent = xmlEventReader.nextEvent();
                  actionName = (xmlEvent.asCharacters().getData());
               }else if(startElement.getName().getLocalPart().equals("caption")){
                   xmlEvent = xmlEventReader.nextEvent();
                   actionCaption = (xmlEvent.asCharacters().getData());
               }else if(startElement.getName().getLocalPart().equals("id")){
                   xmlEvent = xmlEventReader.nextEvent();
                  userId = (xmlEvent.asCharacters().getData());
               }
           }
           //if level end element is reached, add all strings to list
           if(xmlEvent.isEndElement()){
               EndElement endElement = xmlEvent.asEndElement();
               if(endElement.getName().getLocalPart().equals("level")){
                   list.addAll(Arrays.asList(levelName, actionType, actionName, actionCaption, userId));
               }
           }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;

我的输出如下:

Accepted
Approved
Approve
123

Rejected
Rejected by Manager
Reject
123

Rejected
Rejected by Manager
Reject
123

0 个答案:

没有答案