【问题标题】:XML to HTML tag conversionXML 到 HTML 标记的转换
【发布时间】:2017-05-01 01:05:47
【问题描述】:

我有一个带有 XML 标签的字符串。

<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标签(HTML中的大部分东西都是静态的,背景颜色,href等)

我是否必须使用 XML 解析器才能真正实现这一点?一个简单的正则表达式可以完成这项工作吗(我正在使用java)。任何指向此的指针将不胜感激。

【问题讨论】:

    标签: java html regex xml


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-29
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      相关资源
      最近更新 更多