使用SAXHandler在Java中解析XML问题?

时间:2013-12-07 10:17:10

标签: java xml

我有一个XML,如下所示

     <rules>
  <user username= "Alice" priority ="0" >
    <expression>Living Existence Sensor.Alice</expression>
    <objects>
      <object>
        <name>LiveFan</name>
        <action>SetPower</action>
        <value>1.0</value>
      </object>
      <object>
        <name>LiveFan</name>
        <action>SetPower</action>
        <value>1.0</value>
      </object>
      <object>
        <name>LiveFan</name>
        <action>SetPower</action>
        <value>1.0</value>
      </object>
    </objects>
  </user>
   <user username= "John" priority ="1" >
    <expression>Living Existence Sensor.Alice</expression>
    <objects>
      <object>
        <name>LiveFan</name>
        <action>SetPower</action>
        <value>1.0</value>
      </object>
      <object>
        <name>LiveFan</name>
        <action>SetPower</action>
        <value>1.0</value>
      </object>
      <object>
        <name>LiveFan</name>
        <action>SetPower</action>
        <value>1.0</value>
      </object>
    </objects>
  </user>
  <rules>

我这里有两个用户,但我可以拥有多个用户。

我可以保存单个对象。但我无法使用SAXHandler这样做。

在这里,我在对象上做了将保存RULE

public class CustomRules {

    String username;
    String name;
    String action;
    String value;
    }


@Override
  //Triggered when the start of tag is found.
  public void startElement(String uri, String localName, 
                           String qName, Attributes attributes) 
                           throws SAXException {

    switch(qName){
      //Create a new Employee object when the start tag is found
      case "user":
          rule = new CustomRules();
        break;
    }
  }

  @Override
  public void endElement(String uri, String localName, 
                         String qName) throws SAXException {
   switch(qName){
     //Add the employee to list once end tag is found
     case "user":
       empList.add(rule);       
       break;
     //For all other end tags the employee has to be updated.
     case "name":
         rule.name= content;
       break;
     case "action":
         rule.action= content;
         break;
  case "value":
         rule.value= content;
         break;
   }
  }

  @Override
  public void characters(char[] ch, int start, int length) 
          throws SAXException {
    content = String.copyValueOf(ch, start, length).trim();
  }

在这里,我有一个用户名,它保留了很多规则,

所以我该如何解析它。

我试图制作List和一个对象,但我没有成功。

感谢。

2 个答案:

答案 0 :(得分:0)

您想获得所有规则的平面列表吗?如果是这样,您需要触发在“object”标记而不是“user”上添加新规则。

如果你想保留嵌套结构,我建议添加一个“User”类:

class User {
   String username;
   double priority;
   List<CustomRules> customRules = new ArrayList<CustomRules>();
}

添加user变量以跟踪当前用户,类似于rule变量。在“user”开始标记上创建实例,在遇到“用户”结束标记时将其添加到用户列表中。

答案 1 :(得分:0)

这里是解析User列表的示例,其中每个用户都有规则列表。

如果您需要平坦的规则列表,您应该为每个用户节点获取属性“username”并将其保存在实例变量中。创建CustomRules实例应该在startElemnet中移动到case“object”,就像在我的例子中一样。

DefaultHandler handler = new DefaultHandler() {

            class User {
                String username;                    
                List<Rule> list = new ArrayList<Rule>();
            }

            class Rule {
                String name;
                String action;
                String value;
            }

            private User user;
            private Rule rule;
            public List<User> users;

            private String content;

            @Override
            public void startDocument() throws SAXException {
                users = new ArrayList<>();
            }

            @Override
            public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) throws SAXException {
                switch(qName){
                    //Create a new Employee object when the start tag is found
                    case "user":
                        user = new User();
                        user.username = attributes.getValue("username");
                        break;
                    case "object":
                        rule = new Rule();
                        break;
                }
            }

            @Override
            public void endElement(String uri, String localName,
                                   String qName) throws SAXException {
                switch(qName){
                    //Add the employee to list once end tag is found
                    case "user":
                        users.add(user);
                        break;
                    case "object":
                        user.list.add(rule);
                        break;
                    case "name":
                        rule.name= content;
                        break;
                    case "action":
                        rule.action= content;
                        break;
                    case "value":
                        rule.value= content;
                        break;
                }
            }

            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
                content = String.copyValueOf(ch, start, length).trim();
            }
        };