有人有辦法將數據從Atom(XML)格式轉換爲JSON嗎?我寧願有一個免費的在線工具來做到這一點。我無法發佈我想要在線轉換的數據:因爲它包含敏感信息。將原子(XML)轉換爲JSON
回答
「將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要做到這一點,我相信有更多的)。
但是,您隨意使用幕後使用的任何實際轉換器,這些轉換器可能有奇怪的行爲(例如有一天您的源提要添加了一個屬性,並且您的輸出顯着改變)。
爲了減少歧義,這種翻譯應檢查XML的Schema/DTD。 –
我們這裏找到一個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())]) > 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::*) > 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::*) > 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(@*) > 0 and (count(child::*) > 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() < 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() < 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, '
'))"><xsl:copy-of select="$pText"/></xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(substring-before($pText, '
'), ' ')"/>
<xsl:call-template name="removeBreaks">
<xsl:with-param name="pText" select="substring-after($pText, '
')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
希望有所幫助!
儘管此鏈接可能回答此問題,但最好包含答案的重要部分[此處](http://meta.stackoverflow.com/a/8259),並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – bummi
乾杯給我的博客URL) –
@BojanBjelic,您的歡迎! :-) XSLT對我們非常有用。 –
- 1. 將XML轉換爲JSon
- 2. JSON.Net將XML轉換爲JSON
- 3. 將XML轉換爲JSON
- 4. 將JSON轉換爲XML
- 5. Angular2:將XML轉換爲JSON
- 6. Rails將XML轉換爲json
- 7. 使用XML Schema將XML轉換爲JSON
- 8. JS:將XML或JSON轉換爲XML/RDF
- 9. 將JSON轉換爲XML並保存XML
- 10. 出於性能原因,我應該將XML轉換爲JSON嗎?
- 11. 將JAXB XML文件轉換爲JSON
- 12. 使用groovy將JSON轉換爲XML?
- 13. 將XML轉換爲來自Groovy的json
- 14. 將xml數據轉換爲JSON
- 15. 將xml轉換爲json格式
- 16. 將RDF/XML格式轉換爲JSON
- 17. 使用xml_parse_into_struct將XML轉換爲JSON
- 18. 將JSON轉換爲Java中的XML
- 19. 如何將XML轉換爲JSON?
- 20. 將XML字符串轉換爲JSON
- 21. 將xml轉換爲node.js中的json
- 22. 使用R將XML轉換爲JSON
- 23. 在Java中將JSON轉換爲XML
- 24. 在dataweaver mule中將xml轉換爲json
- 25. 使用XSLT將JSON轉換爲XML
- 26. XSLT 3.0,將XML轉換爲JSON
- 27. 使用JavaScriptSerializer將Json轉換爲Xml
- 28. 將JSON轉換爲XML的問題
- 29. 使用Java將xml轉換爲json
- 30. 如何將JSON字典轉換爲XML?
你真的想要什麼格式?只是說「JSON」會讓事情變得模糊,因爲內部結構可能完全是任何事情。 – cloudfeet
@cloudfeet我希望JSON在XML中提供的字段名稱和結構之後進行格式化。只是從Atom(XML)到JSON的直接祕密。 –