代码之家  ›  专栏  ›  技术社区  ›  Ankur

无法向ArrayList添加值,因为它位于内部类中?

  •  1
  • Ankur  · 技术社区  · 16 年前

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

    activeObject.add(file);

    我得到错误“语法错误的令牌,错位的构造(s)”-我不知道这意味着什么。这似乎没有道理,任何帮助都将不胜感激。

    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();
        }
    }
    
    1 回复  |  直到 16 年前
        1
  •  3
  •   Esko    16 年前

    您尝试在匿名类中编写普通语句,就像您尝试了以下操作:

    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);
    }
    

    你不能那样做。如果要在构造时执行此操作,可以将其放入初始值设定项块中,如下所示:

    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);
    
    推荐文章
    Praxder  ·  SAX XMLParser不工作
    13 年前