2013-12-10 50 views
2

有人有办法将数据从Atom(XML)格式转换为JSON吗?我宁愿有一个免费的在线工具来做到这一点。我无法发布我想要在线转换的数据:因为它包含敏感信息。将原子(XML)转换为JSON

+0

你真的想要什么格式?只是说“JSON”会让事情变得模糊,因为内部结构可能完全是任何事情。 – cloudfeet

+0

@cloudfeet我希望JSON在XML中提供的字段名称和结构之后进行格式化。只是从Atom(XML)到JSON的直接秘密。 –

回答

5

“将XML转换为JSON”很简单,但XML和JSON是不同的结构范例。如果您的XML文档如下所示:

<a> 
    <b>foo</b> 
    <c prop="value">bar</c> 
</a> 

......您如何在JSON中表示这一点?有很多问题,比如:

  • 请问订货有关系吗? (在JSON对象属性是无序的,所以转换后没有办法告诉如果<c>之前或<b>来到后)
  • 如果只有一个<b>,这是否意味着它是一个单项目阵列,或只是一个对象?
  • 你如何表现属性?根据属性是否定义,转换器是否在普通字符串(对于"b")和具有附加属性的对象(对于"c")之间翻转?

我见过的每个“XML到JSON转换器”都采取了稍微不同的方法,因此没有“标准”行为依赖。

所以,对于一个完整的答案,我认为你需要给出一个更清晰的想法,你想让你的JSON ATOM格式看起来像。


如果你只是想东西,你会解决什么你,那么你也许能像雅虎管道服务(例如here要做到这一点,我相信有更多的)。

但是,您随意使用幕后使用的任何实际转换器,这些转换器可能有奇怪的行为(例如有一天您的源提要添加了一个属性,并且您的输出显着改变)。

+0

为了减少歧义,这种翻译应检查XML的Schema/DTD。 –

1

我们这里找到一个XSLT:

http://www.bjelic.net/2012/08/01/coding/convert-xml-to-json-using-xslt/#code

,并略作修改它来处理链接Atom文档的&类元素,我们要显示它的方式。

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" encoding="utf-8"/> 

    <xsl:template match="/*[node()]"> 
     <xsl:text>{</xsl:text> 
     <xsl:apply-templates select="." mode="detect" /> 
     <xsl:text>}</xsl:text> 
    </xsl:template> 

    <xsl:template match="*" mode="detect"> 
     <xsl:choose> 
      <xsl:when test="name(preceding-sibling::*[1]) = name(current()) and name(following-sibling::*[1]) != name(current())"> 
        <xsl:apply-templates select="." mode="obj-content" /> 
       <xsl:text>]</xsl:text> 
       <xsl:if test="count(following-sibling::*[name() != name(current())]) &gt; 0">, </xsl:if> 
      </xsl:when> 
      <xsl:when test="name(preceding-sibling::*[1]) = name(current())"> 
        <xsl:apply-templates select="." mode="obj-content" /> 
        <xsl:if test="name(following-sibling::*) = name(current())">, </xsl:if> 
      </xsl:when> 
      <xsl:when test="following-sibling::*[1][name() = name(current())]"> 
       <xsl:text>"</xsl:text><xsl:value-of select="name()"/><xsl:text>" : [</xsl:text> 
        <xsl:apply-templates select="." mode="obj-content" /><xsl:text>, </xsl:text> 
      </xsl:when> 
      <xsl:when test="count(./child::*) > 0 or count(@*) > 0"> 
       <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : <xsl:apply-templates select="." mode="obj-content" /> 
       <xsl:if test="count(following-sibling::*) &gt; 0">, </xsl:if> 
      </xsl:when> 
      <xsl:when test="count(./child::*) = 0"> 
       <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:apply-templates select="."/><xsl:text>"</xsl:text> 
       <xsl:if test="count(following-sibling::*) &gt; 0">, </xsl:if> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:template> 

    <xsl:template match="*" mode="obj-content"> 
     <xsl:text>{</xsl:text> 
      <xsl:apply-templates select="@*" mode="attr" /> 
      <xsl:if test="count(@*) &gt; 0 and (count(child::*) &gt; 0 or text())">, </xsl:if> 
      <xsl:apply-templates select="./*" mode="detect" /> 
      <xsl:if test="count(child::*) = 0 and text() and not(@*)"> 
       <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:value-of select="text()"/><xsl:text>"</xsl:text> 
      </xsl:if> 
      <xsl:if test="count(child::*) = 0 and text() and @*"> 
       <xsl:text>"text" : "</xsl:text><xsl:value-of select="text()"/><xsl:text>"</xsl:text> 
      </xsl:if> 
     <xsl:text>}</xsl:text> 
     <xsl:if test="position() &lt; last()">, </xsl:if> 
    </xsl:template> 

    <xsl:template match="@*" mode="attr"> 
     <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:value-of select="."/><xsl:text>"</xsl:text> 
     <xsl:if test="position() &lt; last()">,</xsl:if> 
    </xsl:template> 

    <xsl:template match="node/@TEXT | text()" name="removeBreaks"> 
     <xsl:param name="pText" select="normalize-space(.)"/> 
     <xsl:choose> 
      <xsl:when test="not(contains($pText, '&#xA;'))"><xsl:copy-of select="$pText"/></xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="concat(substring-before($pText, '&#xD;&#xA;'), ' ')"/> 
       <xsl:call-template name="removeBreaks"> 
        <xsl:with-param name="pText" select="substring-after($pText, '&#xD;&#xA;')"/> 
       </xsl:call-template> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 

希望有所帮助!

+0

尽管此链接可能回答此问题,但最好包含答案的重要部分[此处](http://meta.stackoverflow.com/a/8259),并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – bummi

+0

干杯给我的博客URL) –

+0

@BojanBjelic,您的欢迎! :-) XSLT对我们非常有用。 –