【问题标题】:Comparing two attributes values using SAX and Java使用 SAX 和 Java 比较两个属性值
【发布时间】:2015-04-30 11:38:12
【问题描述】:

这是我的 security.xml

我想使用 SAX 和 java 测试 temperaturet 具有相同的 labelpresencep 具有相同的 label

有人可以帮帮我吗!!?

 <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) if(temp.equalsIgnoreCase("temperature") 有一个 NullPointerException

【问题讨论】:

  • 你是如何继续解决这个问题的?你怎么了?你被什么困住了?
  • 我将我尝试过的内容添加到帖子中
  • 您的示例不完整,因此很难判断问题所在。你有定义 endElement 吗?您的构建器和布尔标志需要重置的地方。字符也是进行比较的错误位置。你需要发布一个 MVCE 并且更清楚你的错误是什么。
  • 我一般如何比较组件的价值!?

标签: java parsing sax


【解决方案1】:

您需要使用 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");
       }
    }

【讨论】:

    【解决方案2】:
     public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
    
                if (qName.equalsIgnoreCase("component")) {
                    String classname = attributes.getValue("classname");
                    String name = attributes.getValue("name");
                    System.out.println("path du componsant= " + classname + "  nom de composant est " + name);
                    bName = true;
                }
                if (qName.equalsIgnoreCase("attribute")) {
                    temp = attributes.getValue("name");
    
                    lab=attributes.getValue("label");
                }
    
            }
    
            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
    
                if (temp != null) {
                    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;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2014-03-22
      • 2020-04-10
      • 1970-01-01
      • 2011-06-23
      • 2010-12-10
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多