2013-12-10 70 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對我們非常有用。 –