How to parse a String containing XML in Java and retrieve the value of the root node?

前端 未结 6 971
小鲜肉
小鲜肉 2020-11-28 06:32

I have XML in the form of a String that contains

HELLO! 

How can I get the String \"Hello!\" from the XML?

6条回答
  •  独厮守ぢ
    2020-11-28 06:53

    You could also use tools provided by the base JRE:

    String msg = "HELLO!";
    DocumentBuilder newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document parse = newDocumentBuilder.parse(new ByteArrayInputStream(msg.getBytes()));
    System.out.println(parse.getFirstChild().getTextContent());
    

提交回复
热议问题