Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用文本解析XML自动关闭标记_Java_Xml_Gate - Fatal编程技术网

Java 用文本解析XML自动关闭标记

Java 用文本解析XML自动关闭标记,java,xml,gate,Java,Xml,Gate,大家好,我正在尝试解析XML文件的这一部分。我遇到的问题是,文本中包含很多自动关闭标记。我无法删除这些标记,因为它们为我提供了一些索引细节。 如果没有所有“节点”标记,我如何访问文本 一名青少年昨天指控他的父母残忍 每天给他喂食薯片,使他体重减轻 在l2岁时膨胀到22岁。 使用XML解析器库,如Jsoup A如何在对该问题的回答中提供: 尽管奇怪,但这种XML实际上格式良好,可以使用普通的XML工具进行解析。TextWithNodes元素只是包含混合内容 TextWithNodes的字符串值可

大家好,我正在尝试解析XML文件的这一部分。我遇到的问题是,文本中包含很多自动关闭标记。我无法删除这些标记,因为它们为我提供了一些索引细节。 如果没有所有“节点”标记,我如何访问文本


一名青少年昨天指控他的父母残忍
每天给他喂食薯片,使他体重减轻
在l2岁时膨胀到22岁。

使用XML解析器库,如Jsoup

A如何在对该问题的回答中提供:

尽管奇怪,但这种XML实际上格式良好,可以使用普通的XML工具进行解析。
TextWithNodes
元素只是包含混合内容

TextWithNodes
的字符串值可以通过简单的XPath获得

string(/TextWithNodes)
生成所需的文本,不带其他标记(自动关闭或其他标记):


下面是一些使用Java中XPATH作为答案的示例代码(归功于@kjhughes):

publicstaticvoidmain(String[]args)抛出IOException、ParserConfigurationException、SAXException、XPathExpressionException{
字符串text=“\n”+
“一名青少年昨天指控他的父母残忍\n”+
“每天给他喂食薯片,这会增加他的体重\n”+
“在l2岁时膨胀到第22位。\n”+
"";
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=builderFactory.newDocumentBuilder();
Document xmlDocument=builder.parse(新的ByteArrayInputStream(text.getBytes(“UTF-8”));
XPath=XPathFactory.newInstance().newXPath();
字符串表达式=“//TextWithNodes”;
System.out.println(xPath.compile(expression.evaluate)(xmlDocument,XPathConstants.STRING));
}
这将打印出:

一名青少年昨天指控他的父母残忍
每天给他喂食薯片,使他体重减轻

在l2岁的时候膨胀到第22位。

谢谢你发布它,我修正了它@埃利奥特谢谢你,但应该归功于克霍斯。他的答案应该是被接受的,而不是我的。我请求你接受克霍斯的回答。接受这个回答是可以的。我很高兴能得到帮助。:-)@kjhughes我只是觉得这不公平:(对你。我已经在你的答案上加了+1。@kjhughes我不知道你们是否可以进一步帮助我,但我可以在文本中加上吗?
string(/TextWithNodes)
 A TEENAGER yesterday 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.
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));
}