Java 获取DOM并按字母顺序重新排列节点和属性
我希望读入XML文档,以确定的顺序重新排列节点和属性,例如:Java 获取DOM并按字母顺序重新排列节点和属性,java,xml,dom,Java,Xml,Dom,我希望读入XML文档,以确定的顺序重新排列节点和属性,例如: <project> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>foobar</artifactId> <version>0.0.1-SNAPSHOT</version> </project>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>foobar</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
4.0.0
通用域名格式
福巴
0.0.1-快照
变成:
<project>
<artifactId>foobar</artifactId>
<groupId>com</groupId>
<modelVersion>4.0.0</modelVersion>
<version>0.0.1-SNAPSHOT</version>
</project>
福巴
通用域名格式
4.0.0
0.0.1-快照
我还想做属性,还有缩进 您可以使用对元素进行排序,我可以推荐JDOM2用于此类工作。您的建议可以通过以下方式实现:
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
public void transformXmlFile(File oldFile, File newFile) throws Exception
{
Document oldDocument = new SAXBuilder().build(oldFile);
Document newDocument = new Document(transformElement(oldDocument.getRootElement()));
List<Element> children = new LinkedList<Element>();
for (Element oldElement : oldDocument.getRootElement().getChildren())
children.add(transformElement(oldElement));
for (Element oldElement : sortElements(children))
newDocument.getRootElement().addContent(oldElement);
XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
serializer.output(newDocument, new FileOutputStream(newFile));
}
private Element transformElement(Element oldElement)
{
Element newElement = new Element(oldElement.getName(), oldElement.getNamespace());
List<Attribute> attributes = new LinkedList<Attribute>();
for (Attribute a: oldElement.getAttributes())
attributes.add(a.clone());
for (Attribute a: sortAttributes(attributes))
newElement.getAttributes().add(a);
return newElement;
}
private List<Attribute> sortAttributes(List<Attribute> attributes)
{
Collections.sort(attributes, new Comparator<Attribute>()
{
@Override
public int compare(Attribute a1, Attribute a2)
{
return a1.getName().compareTo(a2.getName());
}
});
return attributes;
}
private List<Element> sortElements(List<Element> elements)
{
Collections.sort(elements, new Comparator<Element>()
{
@Override
public int compare(Element e1, Element e2)
{
return e1.getName().compareTo(e2.getName());
}
});
return elements;
}
import org.jdom2.Attribute;
导入org.jdom2.Document;
导入org.jdom2.Element;
导入org.jdom2.filter.Filters;
导入org.jdom2.input.SAXBuilder;
导入org.jdom2.output.Format;
导入org.jdom2.output.XMLOutputter;
导入org.jdom2.xpath.XPathExpression;
导入org.jdom2.xpath.XPathFactory;
public void transformXmlFile(File oldFile,File newFile)引发异常
{
Document oldDocument=new SAXBuilder().build(oldFile);
Document newDocument=新文档(transformElement(oldDocument.getRootElement());
列表子项=新建LinkedList();
对于(元素oldElement:oldDocument.getRootElement().getChildren())
添加(transformElement(oldElement));
用于(元素oldElement:sortElements(子元素))
newDocument.getRootElement().addContent(oldElement);
XMLOutputter序列化程序=新的XMLOutputter(Format.getPrettyFormat());
输出(newDocument,newFileOutputStream(newFile));
}
私有元素transformElement(元素oldElement)
{
Element newElement=新元素(oldElement.getName(),oldElement.getNamespace());
列表属性=新建LinkedList();
对于(属性a:oldElement.getAttributes())
attributes.add(a.clone());
for(属性a:sortAttributes(属性))
newElement.getAttributes().add(a);
返回新元素;
}
私有列表分类属性(列表属性)
{
Collections.sort(属性,新的Comparator()
{
@凌驾
公共整数比较(属性a1、属性a2)
{
返回a1.getName().compareTo(a2.getName());
}
});
返回属性;
}
私有列表元素(列表元素)
{
Collections.sort(元素,新的Comparator()
{
@凌驾
公共整数比较(元素e1、元素e2)
{
返回e1.getName().compareTo(e2.getName());
}
});
返回元素;
}
您的示例没有任何属性,通常无法控制属性的顺序。但在XSLT中对元素进行排序很容易:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="name()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>