How to convert String to DOM Document object in java?

前端 未结 4 1772
抹茶落季
抹茶落季 2020-12-15 18:28

I have a case like getting an XML and convert the XML elements to document object and getting the element values and attributes which i have been created already

Her

4条回答
  •  伪装坚强ぢ
    2020-12-15 19:09

    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = db.parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))); //remove the parameter UTF-8 if you don't want to specify the Encoding type.
    

    this works well for me even though the XML structure is complex.

    And please make sure your xmlString is valid for XML, notice the escape character should be added "\" at the front.

    The main problem might not come from the attributes.

提交回复
热议问题