用文本解析 XML 自关闭标签 [英] Parse XML Self-Closing Tags with Text

查看:51
本文介绍了用文本解析 XML 自关闭标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试解析我拥有的 XML 文件的这一部分.我遇到的问题是文本包含很多自关闭标签.我无法删除这些标签,因为它们为我提供了一些索引细节.如何在没有所有节点"标签的情况下访问文本?

Hey everyone I am trying to parse this part of an XML file I have. The problem I am encountering is that the text contains a lot of self-closing tags. I can't remove those tags because they are providing me with some indexing detail. How can I get access to the text without all the "Node" tags?

<TextWithNodes>
 <Node id="0"/>A TEENAGER <Node
id="11"/>yesterday<Node id="20"/> accused his parents of cruelty
by feeding him a daily diet of chips which sent his weight
ballooning to 22st at the age of l2<Node id="146"/>.<Node
id="147"/>
</TextWithNodes>

推荐答案

这里是一些使用 Java 中使用 XPATH 的想法的示例代码在回答 https://stackoverflow.com/a/49926918/2735286(归功于@kjhughes):

Here is some sample code using the idea of using XPATH in Java in answer https://stackoverflow.com/a/49926918/2735286 (credits to @kjhughes):

public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {

    String text = "<TextWithNodes>\n" +
            " <Node id=\"0\"/>A TEENAGER <Node\n" +
            "id=\"11\"/>yesterday<Node id=\"20\"/> accused his parents of cruelty\n" +
            "by feeding him a daily diet of chips which sent his weight\n" +
            "ballooning to 22st at the age of l2<Node id=\"146\"/>.<Node\n" +
            "id=\"147\"/>\n" +
            "</TextWithNodes>";
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document xmlDocument = builder.parse(new ByteArrayInputStream(text.getBytes("UTF-8")));
    XPath xPath = XPathFactory.newInstance().newXPath();
    String expression = "//TextWithNodes";
    System.out.println(xPath.compile(expression).evaluate(xmlDocument, XPathConstants.STRING));
}

打印出来:

一名少年昨天指责他的父母残忍每天喂他吃薯片,这会增加他的体重在 l2 岁时迅速上升到 22 岁.

这篇关于用文本解析 XML 自关闭标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆