【问题标题】:Parsing XML when inside tags vary内部标签变化时解析 XML
【发布时间】:2014-06-20 04:04:15
【问题描述】:

我有一个用例,我想解析以下类型的 XML 文件并从中创建 HTML 报告。

<?xml version = '1.0'?>
<outer tag>
<first_tag>
  <some Random xml tag>
         <another tag>
         </another tag>
  </some Random xml tag>
</first_tag>
<first_tag>
  <some Random xml tag>
         <another tag>
         </another tag>
  </some Random xml tag>
</first_tag>
<second_tag>
  <some Random xml tag>
         <another tag>
         </another tag>
  </some Random xml tag>
</second_tag>

这个XML结构的问题是我只知道整个XML会被包裹在Outer标签中,而二级标签只能是first_tag或者second_Tag。我不知道这些二级标签中会出现什么。

我的 html 应该是这样的:

  Elements inside outer_tag
      First_tag children 
        " <some Random xml tag>
           <another tag>
           </another tag>
         </some Random xml tag>  "  // XML structure itself 
      First_tag children :

        " <some Random xml tag>
           <another tag>
           </another tag>
         </some Random xml tag>  "  // XML structure itself 
        Second_Tag children :

        " <some Random xml tag>
           <another tag>
           </another tag>
         </some Random xml tag>  "  // XML structure itself 

我尝试了 jaxb 和其他 XML 解析器。由于我对内部结构一无所知,我发现很难编写算法。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: xml xml-parsing jaxb


    【解决方案1】:

    XSL 转换在这种情况下会很有效。 XSLT 通常用于将 XML 转换为其他格式,如 HTML。这是一个可以为您提供所需的转换:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output indent="yes"/>
        <xsl:template match="first_tag|second_tag">
            <xsl:copy-of select="./*"/>            
        </xsl:template>
    </xsl:transform>
    

    在这里查看它的工作原理:http://xsltransform.net/jyH9rLS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      • 2013-11-19
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 2020-05-19
      • 2014-03-25
      相关资源
      最近更新 更多