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

使用SAX和Java比较两个属性值,java,parsing,sax,Java,Parsing,Sax,这是我的security.xml 我想使用SAX和java测试temperature和t是否具有相同的标签和presence以及p是否具有相同的标签 有人能帮帮我吗 <root> <component classname="temp.impl.Temperatureservice" name="TemperatureSecurity"> <attributes> <attribute> <name>

这是我的security.xml

我想使用SAX和java测试
temperature
t
是否具有相同的标签
presence
以及
p
是否具有相同的标签

有人能帮帮我吗

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

温度
L
存在
H
T
L
P
H
我尝试的是:

  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;
}
公共类MyHandler扩展了DefaultHandler{
私有列表compList=null;
私有组件comp=null;
布尔bnameatt=true;
布尔bName=true;
布尔blabel=false;
字符串标签;
私有字符串温度;
私人弦实验室;
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、属性)
抛出SAX异常{
if(qName.equalsIgnoreCase(“组件”)){
字符串名称=attributes.getValue(“名称”);
comp=新组件();
公司名称(名称);
//初始化列表
if(compList==null){
compList=newarraylist();
}
}else if(qName.equalsIgnoreCase(“名称”)){
temp=attributes.getValue(“名称”);
bnameatt=真;
}else if(qName.equalsIgnoreCase(“标签”)){
lab=attributes.getValue(“标签”);
布拉贝尔=真;
}
}
@凌驾
public void endElement(字符串uri、字符串localName、字符串qName)引发SAXException{
系统输出打印项次(“温度”+温度+“实验室”+实验室);
if(温度等信号情况(“温度”)){
标签=实验室;
}否则如果(温度等信号情况(“t”)){
系统输出打印项次(“温度”+温度+“实验室”+实验室);
如果(实验室当量(标签)){
系统输出打印项次(“okk”);
}否则{
System.out.println(“不正常”);
}
}
blabel=false;
bnameatt=假;
}
}


我在saax.MyHandler.endElement(MyHandler.java:56)中有一个NullPointerException,如果(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");
       }
    }

你是如何试图继续解决这个问题的?你有什么问题?你有什么困难?我在帖子中添加了我尝试过的内容。你的示例不完整,因此很难判断错误。你是否定义了endElement?在某个地方你的生成器和布尔标志需要重置。此外,字符是进行比较的错误位置。你需要发布一个MVCE,更清楚你的错误是什么。我概述了如何比较组件的价值!?
 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;
            }
        }