从DOM中的XML文件中删除数据?(Remove data from XML file in DOM?)

编程入门 行业动态 更新时间:2024-10-28 18:34:35
从DOM中的XML文件中删除数据?(Remove data from XML file in DOM?)

是否有一种简单的方法(可能使用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>

更多推荐

本文发布于:2023-08-06 01:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   数据   XML   DOM   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!