如何按类型获取xml节点?

时间:2015-07-23 10:21:32

标签: java xml xml-parsing html-parsing

我有这个xml:

<html>
<head><title>504 Gateway Time-out</title></head>
<body bgcolor="white">
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>

我怎样才能收到这个文字?

504网关超时。 nginx / 1.4.6(Ubuntu)

我曾尝试编写xml解析器,但我不确定如何按类型选择节点?

@Override
public String getProp(String prop) {
    String answer = null;
    try {
        final Document document = loadXMLFromString();
        Element root = document.getDocumentElement();
        answer = root.getElementsByTagName(prop).item(0).getTextContent();
    } catch (Exception e) {
        //      e.printStackTrace();
    }
    return answer;
}

private Document loadXMLFromString() throws Exception
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xmlString));
    return builder.parse(is);
}

0 个答案:

没有答案