【发布时间】:2015-04-30 11:38:12
【问题描述】:
这是我的 security.xml
我想使用 SAX 和 java 测试 temperature 和 t 具有相同的 label 和 presence 和 p 具有相同的 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 并且更清楚你的错误是什么。
-
我一般如何比较组件的价值!?