2010-05-25 61 views
1

我在保存xml文档对象,并将其保存在xml文件中,如下所示。保存xml对象,以便元素在保存的xml文件中按排序顺序排列

<author name="tom" book="Fun-II"/> 
<author name="jack" book="Live-I"/> 
<author name="pete" book="Code-I"/> 
<author name="jack" book="Live-II"/> 
<author name="pete" book="Code-II"/> 
<author name="tom" book="Fun-I"/> 

,而不是我想这样,当我坚持它是由如下分组然后提交书的名称保存对象在文档对象的内容进行排序:

<author name="jack" book="Live-I"/> 
<author name="jack" book="Live-II"/> 
<author name="pete" book="Code-I"/> 
<author name="pete" book="Code-II"/> 
<author name="tom" book="Fun-I"/> 
<author name="tom" book="Fun-II"/> 

我使用Apache XML豆..关于如何实现这一目标的任何想法?

谢谢。

回答

3

XML没有排序顺序,您可以通过使用XSLT 类似的东西转换XML:

<xsl:for-each select="author" order-by="+ name"> 
<tr> 
    <td><xsl:value-of select="@name"/></td> 
    <td><xsl:value-of select="@book"/></td> 
</tr> 
</xsl:for-each> 

Sorting in XSLT见furhter想法。

0

作为堆栈器已经提到,普通的xml文件永远不会(通常是?)没有排序或排序。要对XML文档进行排序,您可以在序列化之前对模型进行排序,或者创建/使用外部排序器来处理现有的xml文档。

0

还应当指出的是,元素

<author name="pete" book="Code-I"/> 

是相同的:

<author book="Code-I" name="pete"/> 

属性节点,不像文本节点和元素节点没有顺序。因此,出于XML的目的,您必须告诉它应该对哪个属性进行排序。