Xml not parsing String as input with sax

后端 未结 5 1402
灰色年华
灰色年华 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:19

    Mybe this help. it's uses javax.xml.parsers.DocumentBuilder, which is easier than SAX

    public Document getDomElement(String xml){
            Document doc = null;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            try {
    
                DocumentBuilder db = dbf.newDocumentBuilder();
    
                InputSource is = new InputSource();
                    is.setCharacterStream(new StringReader(xml));
                    doc = db.parse(is); 
    
                } catch (ParserConfigurationException e) {
                    Log.e("Error: ", e.getMessage());
                    return null;
                } catch (SAXException e) {
                    Log.e("Error: ", e.getMessage());
                    return null;
                } catch (IOException e) {
                    Log.e("Error: ", e.getMessage());
                    return null;
                }
                    // return DOM
                return doc;
        }
    

    you can loop through the document by using NodeList and check each Node by it's name

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题