Xml not parsing String as input with sax

后端 未结 5 1392
灰色年华
灰色年华 2021-02-14 10:31

I have a string input from which I need to extract simple information, here is the sample xml (from mkyong):



    &l         


        
5条回答
  •  被撕碎了的回忆
    2021-02-14 11:28

    You call parse with a String as the first parameter. According to the docu that string is interpreted as the URI to your file.

    If you want to parse your String directly, you have to transform it to an InputStream in the first place for usage with the parse(InputSource is, DefaultHandler dh) method (docu):

    // transform from string to inputstream
    ByteArrayInputStream in = new ByteArrayInputStream(xml.toString().getBytes());
    InputSource is = new InputSource();
    is.setByteStream(in);
    
    // start parsing
    saxParser.parse(xml.toString(), handler);
    

提交回复
热议问题