我是以下字符串:
<table:table-cell table:style-name="Table2.A1" office:value-type="string">
<text:p text:style-name="P32">
<text:span text:style-name="T1">test description</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">17/07/2013</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T3"></text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T3">test <!-- end tag is missing -->
</text:p>
</table:table-cell>
有没有办法找到未关闭的标签并插入?
预期产出:
<table:table-cell table:style-name="Table2.A1" office:value-type="string">
<text:p text:style-name="P32">
<text:span text:style-name="T1">test description</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">17/07/2013</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T3"></text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T3">test</text:span>
</text:p>
</table:table-cell>
提前致谢
答案 0 :(得分:2)
是。这很可能。
软件工程/数据结构中的基本问题。 使用堆栈维护标记并检查它们是否正确关闭。
我提出了基本的想法,这是解决问题的方法
答案 1 :(得分:1)
一个非常简单且可行的解决方案是使用任何可用的宽松“html”SAXreader:
我相信两者都提供(我确定感知器确实)XmlReader实现非常宽容他们接受什么样的“野蛮”“HTML”,并且他们将始终生成格式良好的XML(XHTML)。例如,这就是你如何使用DOM4J和TagSoup来“纠正”无效输入。
SAXReader reader = new SAXReader(
org.ccil.cowan.tagsoup.Parser.class.getName());
Document doc = reader.read(...);
XMLWriter writer = new XMLWriter(System.out);
writer.write(doc);
根据您的输入,它会产生:
<table:table-cell xmlns:table="urn:x-prefix:table" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:office="urn:x-prefix:office" table:style-name="Table2.A1" office:value-type="string">
<text:p xmlns:text="urn:x-prefix:text" text:style-name="P32">
<text:span text:style-name="T1">test description</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">17/07/2013</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T2"> </text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T3"></text:span>
<text:span text:style-name="T1">test</text:span>
<text:span text:style-name="T3">test <!-- end tag is missing -->
</text:span></text:p>
</table:table-cell>