使用 SAX 和 Java 比较两个属性值
Comparing two attributes values using SAX and Java
这是我的security.xml
我想测试temperature
和t
有相同的标签和presence
和p
有相同的label 使用 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) if(temp.equalsIgnoreCase("temperature")
有一个 NullPointerException
您需要使用 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");
}
}
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;
}
}
这是我的security.xml
我想测试temperature
和t
有相同的标签和presence
和p
有相同的label 使用 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) 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");
}
}
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;
}
}