Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 停止分析sax并返回列表_Java_Sax - Fatal编程技术网

Java 停止分析sax并返回列表

Java 停止分析sax并返回列表,java,sax,Java,Sax,我正在用sax解析器解析一个大的xml文档,我要做的是在找到特定元素时停止解析,并返回我在解析文件时填充的ArrayList。 怎么办 我已尝试创建异常,但无法返回正在填写的列表 我的问题还有别的解决办法吗 这是我的功能 public List<String> parse(String id) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException { Li

我正在用sax解析器解析一个大的xml文档,我要做的是在找到特定元素时停止解析,并返回我在解析文件时填充的ArrayList。 怎么办

我已尝试创建异常,但无法返回正在填写的列表 我的问题还有别的解决办法吗

这是我的功能

public List<String> parse(String id) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException
{

    List<String> list_Lat_Lon = new ArrayList<String>();

    //****************************************************************************************************

    try{


    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true); //--- or false
    SAXParser saxParser = factory.newSAXParser();


    DefaultHandler content_handler;
    content_handler = new DefaultHandler() {



    @Override
    public void startDocument() throws SAXException {

    }  

       @Override
   public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException
   {
       if(qName.equals("node")){

           if(attributes.getValue("id").equals(id)){
               String lat = attributes.getValue("lat");
               String lon = attributes.getValue("lon");

               list_Lat_Lon.add(lat);
               list_Lat_Lon.add(lon); // filling my List


           }
       }
       if(qName.equals("way")){
          **// here i want to break my parsing and return my List**
       }

   }


       @Override
   public void endElement(String uri, String localName, String qName)throws SAXException
   {



   }

       @Override
   public void characters(char ch[], int start, int length)throws SAXException
   {

   }

       @Override
    public void endDocument() throws SAXException {


    }



};
   saxParser.parse("src/ParsingOSM/algeria-latest.osm", content_handler); 
    }catch (ParserConfigurationException e) {
         System.out.println(e.toString());      
        }

   return list_Lat_Lon; 
}
公共列表解析(字符串id)抛出ParserConfiguration异常、SAXException、IOException、ClassNotFoundException
{
List List_Lat_Lon=new ArrayList();
//****************************************************************************************************
试一试{
SAXParserFactory=SAXParserFactory.newInstance();
factory.setValidating(true);//---或false
SAXParser SAXParser=factory.newSAXParser();
DefaultHandler内容\u处理程序;
content\u handler=newdefaulthandler(){
@凌驾
public void startDocument()引发异常{
}  
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、属性)引发SAXException
{
if(qName.equals(“节点”)){
if(attributes.getValue(“id”).equals(id)){
字符串lat=attributes.getValue(“lat”);
字符串lon=attributes.getValue(“lon”);
列表横向长度。添加(横向);
list_Lat_Lon.add(Lon);//填写我的列表
}
}
if(qName.equals(“方式”)){
**//在这里,我想中断我的解析并返回我的列表**
}
}
@凌驾
public void endElement(字符串uri、字符串localName、字符串qName)引发SAXException
{
}
@凌驾
公共无效字符(char ch[],int start,int length)引发异常
{
}
@凌驾
public void endDocument()引发异常{
}
};
parse(“src/ParsingOSM/alegaria latest.osm”,content\u handler);
}捕获(ParserConfiguration异常e){
System.out.println(例如toString());
}
返回列表_Lat_Lon;
}

您可以使用
LinkedHashMap
(使用
lat
lon
键时,插入顺序将保留)。因为您希望在第一次出现时返回。我想这和元素的出现顺序有关。将元素值放入映射中,并在解析完成后对其进行操作

编辑


您说过您已经尝试在条件满足时抛出一个
异常。当抛出
异常
抛出新SAXException()
时,无法返回值列表。一个肮脏的技巧可能是,将字符串值附加到异常消息,然后在
catch
块中重试。事实上,这不是一个肮脏的解决方案,因为只有当
条件匹配时才会抛出异常。这就是你想要的。此外,您可能会考虑使用自定义异常,因为您可以准确地捕获它。希望这能解决你的问题。

你可以考虑使用@ Johannes Kuhn的可能副本,在这里他需要列表作为返回值,同时退出解析。“复制问题”的链接没有说明返回任何对象。您可能想阅读我编辑的答案。子类SAXException,添加一个包含返回值的字段,捕获子类,提取值。不,您可以退出解析。感谢您的回答,我理解我不能基于某些条件强制SAX返回的事实,但现在我想在条件为真时停止解析,并在中断解析后对映射进行操作,因为文件太复杂big@TarekAbdellaziz马克后来更新了他们的答案。这能解决你的问题吗?@TarekAbdellaziz,我的回答能解决你的问题吗?如果是,您接受它作为答案,或者提供其他原因。