在 Java 中解析没有结束标记的 XML

编程入门 行业动态 更新时间:2024-10-27 20:30:00
本文介绍了在 Java 中解析没有结束标记的 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在解析没有结束标记的 XML 时遇到问题.请参阅下面的 xml 片段.

I am having trouble parsing an XML with no closing tag. Please see snippet of the xml below.

我尝试过 SAX 和 StAX 解析器,它们都需要一个格式正确的 XML 和结束标记 XXYY....正如您在下面看到的,XML 格式有点不同...如果有任何 API,请帮助我那里可以帮助我解析这个或者如果 SAX/StAX 可以帮助我实现我想要的...... :(

I have tried SAX and also StAX Parser they both need a properly formatted XML with closing tag XXYY....as you can see below the XML format is a little bit different... Please help me if there is any API out there that can help me parse this or if SAX/StAX can help me achieve what I want.... :(

<Employees>
 <Employee>
  <Detail>
    <Date>2018014
    <Name>XXYY
    <Age>0
    <LANGUAGE>ENG
    <Manager>
    <MName>YYXX
    <MID>5959
    </Manager>
    <EmployeeID>1234
  </Detail>
 </Employee>
</Employees>

推荐答案

您可以通过添加所有缺失的结束标记来修复" XML.

You could "fix" the XML by adding all the missing end-tags.

任何在同一行的标签后包含文本的开始标签都可以通过在行尾添加结束标签来修复.

Any start-tag that contains text after the tag, on the same line, could be fixed by adding an end-tag at the end of the line.

包含文本"的规则确保例如<Manager> 标签没有结束,因为它实际上是向下 3 行结束.

The rule of "contains text" ensures that e.g. the <Manager> tag doesn't get ended, since that is actually ended 3 lines down.

示例工作代码:

// Load file into memory
String xml = new String(Files.readAllBytes(Paths.get("test.xml")), StandardCharsets.UTF_8);

// Apply magic to add missing end-tags
xml = xml.replaceAll("(?m)^(\\s*)<(\\w+)>([^<]+)$", "$1<$2>$3</$2>");

// Parse then print the XML, to ensure there are no errors
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                                          .parse(new InputSource(new StringReader(xml)));
TransformerFactory.newInstance().newTransformer()
                  .transform(new DOMSource(document), new StreamResult(System.out));

这篇关于在 Java 中解析没有结束标记的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 02:44:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1054525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   结束   Java   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!