Search code examples
javaparsingsax

Comparing two attributes values using SAX and Java


This is my security.xml

I want to test temperature and t have the same label and presence and p have the same label using SAX and java.

could someone help me !!?

 <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>

what i tried :

  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;
}

}

i have a NullPointerException at saax.MyHandler.endElement(MyHandler.java:56) if(temp.equalsIgnoreCase("temperature")


Solution

  •  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;
                }
            }