XML到HTML标记转换

时间:2016-12-15 12:17:11

标签: java html regex xml

我有一个带有XML标签的String。

<PERSON>Obama</PERSON> is the current <POSITION>President</POSITION> of <LOCATION>United States of America<LOCATION>

我想将这些标签转换为表格

<p><span style="background-color: #99cc00;"><a style="background-color: #99cc00;" title="PERSON" href="http://usa.com">OBAMA</a></span> is the current <span style="background-color: #99cc00;"><a style="background-color: #99cc00;" title="POSITION" href="http://usa.com">President</a></span> of <span style="background-color: #99cc00;"><a style="background-color: #99cc00;" title="LOCATION" href="http://usa.com">United States of America</a></span></p>

这里的问题是用HTML标签替换XML TAG(HTML中的大部分内容都是静态的,如背景颜色,href等等。)

我是否必须使用XML解析器来实现此目的?一个简单的正则表达式可以完成这项工作(我使用java)。任何指向此的指针都将受到赞赏。

1 个答案:

答案 0 :(得分:1)

你只需要解析字符串。并搜索正确的位置。

String person -> cut the string between <Person> and </Person>
String desc1 = cut between </Person> and <Position>

... 等

String.format("<p><span style="background-color: #99cc00;"><a style="background-color: #99cc00;" title="PERSON" href="http://usa.com">%s</a></span> %s <span style="background-color: #99cc00;"><a style="background-color: #99cc00;" title="POSITION" href="http://usa.com">President</a></span> %s <span style="background-color: #99cc00;"><a style="background-color: #99cc00;" title="LOCATION" href="http://usa.com">%s</a></span></p>"
,person, desc1, position, desc2, location)