Java SAXParser'中的参数;startElement&x27;方法始终为空
我试图实现一个sax解析器来加载一些XML。但是,DefaultHandler内的方法中的所有参数始终为null。例如调用Java SAXParser'中的参数;startElement&x27;方法始终为空,java,xml,saxparser,Java,Xml,Saxparser,我试图实现一个sax解析器来加载一些XML。但是,DefaultHandler内的方法中的所有参数始终为null。例如调用System.out.println(uri)打印空值。代码如下。运行代码时,不会在任何位置打印异常/错误。任何想法/帮助都将不胜感激 默认处理程序: try { handler = new DefaultHandler(){ int channel_amt = 0; public void star
System.out.println(uri)startElement
方法内部的code>打印空值。代码如下。运行代码时,不会在任何位置打印异常/错误。任何想法/帮助都将不胜感激
默认处理程序:
try {
handler = new DefaultHandler(){
int channel_amt = 0;
public void startElement(String uri, String localname, String name, Attributes attributes) throws SAXException {
if(name == "fixture"){
profileID++;
System.out.println(attributes.getValue(0));
profile_name = attributes.getValue(0);
profile_mode = attributes.getValue(1);
profile_channels.clear();
} else {
if(name == "channel"){
profile_channel = new Vector<Object>();
profile_channel.addElement(attributes.getValue(0));
profile_channel.addElement(attributes.getValue(1));
// profile_channel.addElement(Boolean.parseBoolean(attributes.getValue(2)));
channel_amt++;
if(Arrays.asList(channels).indexOf(attributes.getValue(1)) != -1){
profile_channel_function[Arrays.asList(channels).indexOf(attributes.getValue(1))] = channel_amt;
}
} else {
profile_channel.addElement(new Range(Integer.parseInt(attributes.getValue(0)), Integer.parseInt(attributes.getValue(1)), attributes.getValue(2)));
}
}
}
public void endElement(String uri, String localname, String name) throws SAXException {
if(name == "fixture"){
profile[profileID] = new Profile(profile_name, profile_mode, profile_channels, profile_built_in_dimmer, profile_channel_function);
} else if(name == "channel"){
profile_channels.add(profile_channel);
}
}
};
loadProfile("Dimmer.xml");
} catch(Exception e){
e.printStackTrace();
}
public void loadProfile(String name){
for(int c=0;c<51;c++){
profile_channel_function[c] = 0;
}
SAXParser parser;
try {
parser = factory.newSAXParser();
parser.parse(new File(name), handler);
} catch (Exception e) {
e.printStackTrace();
}
}
试试看{
handler=新的DefaultHandler(){
int信道_amt=0;
public void startElement(字符串uri、字符串localname、字符串名称、属性)引发SAXException{
如果(名称=“夹具”){
profileID++;
System.out.println(attributes.getValue(0));
profile_name=attributes.getValue(0);
profile_mode=attributes.getValue(1);
profile_channels.clear();
}否则{
如果(名称=“频道”){
profile_channel=新向量();
profile_channel.addElement(attributes.getValue(0));
profile_channel.addElement(attributes.getValue(1));
//profile_channel.addElement(Boolean.parseBoolean(attributes.getValue(2));
通道μamt++;
if(Arrays.asList(channels).indexOf(attributes.getValue(1))!=-1){
profile_channel_函数[Arrays.asList(channels).indexOf(attributes.getValue(1))]=channel_amt;
}
}否则{
profile_channel.addElement(新范围(Integer.parseInt(attributes.getValue(0))、Integer.parseInt(attributes.getValue(1))、attributes.getValue(2));
}
}
}
公共void endElement(字符串uri、字符串localname、字符串名称)引发SAXException{
如果(名称=“夹具”){
配置文件[profileID]=新配置文件(配置文件名称、配置文件模式、配置文件通道、配置文件内置调光器、配置文件通道功能);
}else if(名称==“频道”){
配置文件频道。添加(配置文件频道);
}
}
};
loadProfile(“Dimmer.xml”);
}捕获(例外e){
e、 printStackTrace();
}
加载配置文件方法:
try {
handler = new DefaultHandler(){
int channel_amt = 0;
public void startElement(String uri, String localname, String name, Attributes attributes) throws SAXException {
if(name == "fixture"){
profileID++;
System.out.println(attributes.getValue(0));
profile_name = attributes.getValue(0);
profile_mode = attributes.getValue(1);
profile_channels.clear();
} else {
if(name == "channel"){
profile_channel = new Vector<Object>();
profile_channel.addElement(attributes.getValue(0));
profile_channel.addElement(attributes.getValue(1));
// profile_channel.addElement(Boolean.parseBoolean(attributes.getValue(2)));
channel_amt++;
if(Arrays.asList(channels).indexOf(attributes.getValue(1)) != -1){
profile_channel_function[Arrays.asList(channels).indexOf(attributes.getValue(1))] = channel_amt;
}
} else {
profile_channel.addElement(new Range(Integer.parseInt(attributes.getValue(0)), Integer.parseInt(attributes.getValue(1)), attributes.getValue(2)));
}
}
}
public void endElement(String uri, String localname, String name) throws SAXException {
if(name == "fixture"){
profile[profileID] = new Profile(profile_name, profile_mode, profile_channels, profile_built_in_dimmer, profile_channel_function);
} else if(name == "channel"){
profile_channels.add(profile_channel);
}
}
};
loadProfile("Dimmer.xml");
} catch(Exception e){
e.printStackTrace();
}
public void loadProfile(String name){
for(int c=0;c<51;c++){
profile_channel_function[c] = 0;
}
SAXParser parser;
try {
parser = factory.newSAXParser();
parser.parse(new File(name), handler);
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadProfile(字符串名称){
对于(int c=0;cDon),请不要使用=
来比较字符串。请使用equals()
方法。@jsheeran这让我找到了解决方案!谢谢