是否有一种简单的方法(可能使用DOM api或其他),我可以从XML文件中删除实际数据,只留下其模式的一种模板,以便我们可以看到它可以容纳哪些潜在信息。
我会举一个例子,说清楚。
考虑用户输入以下xml文件:
<photos page="2" pages="89" perpage="10" total="881"> <photo id="2636" owner="47058503995@N01" secret="a123456" server="2" title="test_04" ispublic="1" isfriend="0" isfamily="0" /> <photo id="2635" owner="47058503995@N01" secret="b123456" server="2" title="test_03" ispublic="0" isfriend="1" isfamily="1" /> <photo id="2633" owner="47058503995@N01" secret="c123456" server="2" title="test_01" ispublic="1" isfriend="0" isfamily="0" /> <photo id="2610" owner="12037949754@N01" secret="d123456" server="2" title="00_tall" ispublic="1" isfriend="0" isfamily="0" /> </photos>然后我想将其转换为:
<photos page=“..." pages=“..." perpage=“..." total=“..."> <photo id=“.." owner=“.." secret=“..." server=“..." title=“..." ispublic=“..." isfriend=“..." isfamily=“...” /> </photos>我确信这可以手动编写,但这将是最好,最有效和最可靠的方法。 (最好是Java)。
日Thnx!
Is there an easy way (perhaps using the DOM api, or other) where I could remove the actual data from an XML file, leaving behind just a kind of template of its schema, so that we can see what potential information it can hold.
I will give an example, to make this clear.
Consider the users inputs the following xml file:
<photos page="2" pages="89" perpage="10" total="881"> <photo id="2636" owner="47058503995@N01" secret="a123456" server="2" title="test_04" ispublic="1" isfriend="0" isfamily="0" /> <photo id="2635" owner="47058503995@N01" secret="b123456" server="2" title="test_03" ispublic="0" isfriend="1" isfamily="1" /> <photo id="2633" owner="47058503995@N01" secret="c123456" server="2" title="test_01" ispublic="1" isfriend="0" isfamily="0" /> <photo id="2610" owner="12037949754@N01" secret="d123456" server="2" title="00_tall" ispublic="1" isfriend="0" isfamily="0" /> </photos>Then I want to transform this into:
<photos page=“..." pages=“..." perpage=“..." total=“..."> <photo id=“.." owner=“.." secret=“..." server=“..." title=“..." ispublic=“..." isfriend=“..." isfamily=“...” /> </photos>I’m sure this could be written manually, but would be the be best, most efficient and reliable way of doing this. (preferably in Java).
Thnx!
最满意答案
有很多可能性:
DOM API(包含在JDK中) SAX API(包含在JDK中) JDOM(易于使用,但外部) XSLT(使用准备好的XSL样式表转换XML,JDK支持XSLT 1.0)我认为XSLT是将XML转换为另一种XML的最可靠和通用的方法。 这是一些简单的例子:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="node()"> <xsl:copy> <xsl:apply-templates select="@*|node()[position()=1]"/> </xsl:copy> </xsl:template> <xsl:template match="@*"> <xsl:attribute name="{name()}">...</xsl:attribute> </xsl:template> </xsl:stylesheet>结果:
<photos page="..." pages="..." perpage="..." total="..."> <photo id="..." owner="..." secret="..." server="..." title="..." ispublic="..." isfriend="..." isfamily="..."/> </photos>There are plenty of possibilities:
DOM API (included in JDK) SAX API (included in JDK) JDOM (easy to use, but external) XSLT (transforming XML with prepared XSL stylesheet, JDK supports XSLT 1.0)I think that XSLT is most reliable and universal way to transform XML into another XML. Here is some quick example:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="node()"> <xsl:copy> <xsl:apply-templates select="@*|node()[position()=1]"/> </xsl:copy> </xsl:template> <xsl:template match="@*"> <xsl:attribute name="{name()}">...</xsl:attribute> </xsl:template> </xsl:stylesheet>Result:
<photos page="..." pages="..." perpage="..." total="..."> <photo id="..." owner="..." secret="..." server="..." title="..." ispublic="..." isfriend="..." isfamily="..."/> </photos>更多推荐
发布评论