How to create a XML object from String in Java?

后端 未结 2 1974
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 20:15

I am trying to write a code that helps me to create a XML object. For example, I will give a string as input to a function and it will return me a XMLObject.



        
2条回答
  •  失恋的感觉
    2020-12-02 20:46

    try something like

    public static Document loadXML(String xml) throws Exception
    {
       DocumentBuilderFactory fctr = DocumentBuilderFactory.newInstance();
       DocumentBuilder bldr = fctr.newDocumentBuilder();
       InputSource insrc = new InputSource(new StringReader(xml));
       return bldr.parse(insrc);
    }
    

提交回复
热议问题