java - 使用 SAX 和 Java 比较两个属性值

标签 java parsing sax

这是我的 security.xml

我想测试温度t具有相同的标签存在p 使用 SAX 和 java 具有相同的标签

有人可以帮我吗!!?

 <root>

   <component classname="temp.impl.Temperatureservice" name="TemperatureSecurity">
<attributes>
       <attribute>

     <name> temperature</name>

        <label>L</label>

     </attribute>
 </attributes>
     </component> 


  <component classname="pres.impl.Presenceservice" name="PresenceSecurity">
<attributes>
    <attribute> 

<name>presence</name>

      <label>H</label>

   </attribute>
   </attributes>
 </component>

 <component classname="anal.AnalyserService" name="ManagerSecurity">
 <attributes>
       <attribute >

      <name>t</name>

        <label>L</label>

      </attribute>

<attribute>

 <name>p</name>

     <label>H</label>

        </attribute>
</attributes>
   </component>  


</root>

我尝试过:

  public class MyHandler extends DefaultHandler {

private List<Component> compList = null;
private Component comp = null;
boolean bnameatt = true;
boolean bName = true;
boolean blabel = false;
String label;
private String temp;
private String lab;

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

    if (qName.equalsIgnoreCase("component")) {

        String name = attributes.getValue("name");

        comp = new Component();
        comp.setName(name);
        //initialize list
        if (compList == null) {
            compList = new ArrayList<>();
        }
    } else if (qName.equalsIgnoreCase("name")) {
        temp = attributes.getValue("name");
        bnameatt = true;
    } else if (qName.equalsIgnoreCase("label")) {
        lab = attributes.getValue("label");
        blabel = true;

    }
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    System.out.println("temp " + temp + " lab " + lab);
    if (temp.equalsIgnoreCase("temperature")) {

        label = lab;
    } else if (temp.equalsIgnoreCase("t")) {
        System.out.println("temp " + temp + " lab " + lab);
        if (lab.equals(label)) {
            System.out.println("okk");
        } else {
            System.out.println("not ok");
        }
    }
    blabel=false;
    bnameatt=false;
}

}

我在 saax.MyHandler.endElement(MyHandler.java:56) 有一个 NullPointerException if(temp.equalsIgnoreCase("temperature")

最佳答案

您需要使用 endElement 回调来保存使用 builder 和 builder1 构建的内容,然后将其与之前的结果进行比较。像这样的东西:

  public void endElement(String uri, String localName, String qName) {
       if (builder.toString().equals("temperature")) 
           temprature_label = builder1.toString();
       else if (builder.toString().equals("t")) {
            if (builder1.toString().equals(temprature_label)) 
                System.out.println("okkk");
            else
                System.out.println("not ok");
       }
    }

关于java - 使用 SAX 和 Java 比较两个属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29966721/

相关文章:

java - Eclipse Maven Spring : Server Error when I try to Run As Server (Tomcat 7)

java - 如何建立生产者-产品关系

c# - 从 weather api 解析 json

ruby - open-uri + hpricot 和 nokogiri 不能正确解析 html

objective-c - 在主线程或后台线程上进行iOS XML解析

java - 如何让 Spring Security 将 HTTP session 存储在数据库中以在多个服务器上使用 Web 应用程序?

java - HTML WebView 上使用键盘时如何隐藏 ImageView

parsing - 将数学函数转换为 lua

xml - 使用 SAX 解析 XML 字符串

java - 将正则表达式应用于 SAX 流