gpt4 book ai didi

java - 无法向 ArrayList 添加值,因为它在内部类中?

转载 作者:行者123 更新时间:2023-11-29 06:20:31 30 4
gpt4 key购买 nike

我正在编写一个可以解析 rdf 和 owl 文件的文件。我正在使用 SAX 和 Java。

我的问题在activeObject.add(file);

这行

我收到错误“标记语法错误,错误放置的构造”——我不知道这是什么意思。这似乎没有意义,我们将不胜感激。

PS:我可能对导致错误的原因完全错误,它可能与内部类无关。

public static void main(String args[]) throws URISyntaxException, MalformedURLException, IOException {

// String file =
// "http://www.srdc.metu.edu.tr/ubl/contextOntology/cpc.owl";
// final String file = "http://onto.eva.mpg.de/obo/image.owl";
// final String file =
// "http://www.informatik.uni-ulm.de/ki/Liebig/owl/SUMO-LiteTB.rdf";
// final String file =
// "http://www.csd.abdn.ac.uk/~apreece/research/TowardsAnIntelligentWeb/PUB_findallbyKen_result.rdf";
// final String file =
// "http://www.srdc.metu.edu.tr/ubl/contextOntology/naics.owl";
final String file = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

URI uri = new URI(file);
InputStream is = uri.toURL().openStream();

try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {

ArrayList<String> activeProperty = new ArrayList<String>();
ArrayList<Triple> triplesList = new ArrayList<Triple>();
ArrayList<String> activeObject = new ArrayList<String>();

boolean name = false;
activeObject.add(file);

public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException {

activeProperty.add(qName);
int attrLength = attributes.getLength();

for (int i = 0; i < attrLength; i++) {
String attrName = attributes.getQName(i).toLowerCase();
String attrValue = attributes.getValue(i).toLowerCase();

if (attrName.equals("rdf:about") || attrName.equals("rdf:resource") || attrName.equals("rdf:id")) {
activeObject.add(attrValue);
System.out.println(activeObject);
}

else{
String subject = activeObject.get(activeObject.size()-1);
String predicate = attrName;
String object = attrValue;
Triple newTriple = new Triple(subject, predicate, object);
}
}
}

public void characters(char[] ch, int start, int length) throws SAXException {
// tempVal = new String(ch, start, length);
}

public void endElement(String uri, String localName, String rawName) {

String subject = activeObject.get(activeObject.size()-2);
String predicate = activeProperty.get(activeProperty.size()-1);
String object = activeObject.get(activeObject.size()-1);

Triple newTriple = new Triple(subject,predicate, object);

if (rawName.equals(activeProperty.get(activeProperty.size() - 1))) {
activeProperty.remove(activeProperty.size() - 1);
}
else{
System.out.println("Something is seriosuly wrong ...sdf.sdf.we8ryw98fsydh");
}
// System.out.println(activeProperty);
}

// if (qName.equalsIgnoreCase("NAME")) {
// name = true;
// }

// public void characters(char ch[], int start, int length)
// throws SAXException {
// if (name) {
// System.out.println("Name: "
// + new String(ch, start, length));
// name = false;
// }
// }
};

saxParser.parse(is, handler);

} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

您正在尝试在匿名类中编写普通语句,就好像您已经尝试过这样:

class Foo extends DefaultHandler {
ArrayList<String> activeProperty = new ArrayList<String>();
ArrayList<Triple> triplesList = new ArrayList<Triple>();
ArrayList<String> activeObject = new ArrayList<String>();

boolean name = false;
activeObject.add(file);
}

你不能那样做。如果你想在构建时执行它,你可以把它放在一个初始化 block 中,像这样:

ArrayList<String> activeProperty = new ArrayList<String>();
ArrayList<Triple> triplesList = new ArrayList<Triple>();
ArrayList<String> activeObject = new ArrayList<String>();
boolean name = false;

{
activeObject.add(file);
}

或者您可以用其他方式填充列表,也许 - 例如 Guava你可以这样写:

ArrayList<String> activeObject = Lists.newArrayList(file);

关于java - 无法向 ArrayList 添加值,因为它在内部类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3244733/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com