如何在使用Java修改XML文件后停止XML标记属性在升序中排序?

时间:2015-06-08 15:57:19

标签: java xml dom jaxb xml-formatting

我编写了一个程序来查找与目录中特定模式匹配的所有XML文件,并通过添加新标记对其进行修改。



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<paths>
  <upgradepath startversion="1.4.0.0" moduleid="${moduleId}" endversion="1.4.0.1">
    <steps>
      <!-- Put scripts here to go from 1.4.0.0 to 1.4.0.1 -->
    </steps>
  </upgradepath>

  <upgradepath startversion="1.4.0.1" moduleid="${moduleId}" endversion="1.4.0.2">
    <steps>
      <!-- Put scripts here to go from 1.4.0.1 to 1.4.0.2 -->
    </steps>
  </upgradepath>
</paths>
&#13;
&#13;
&#13;

运行我的程序后,XML文件被修改如下:

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<paths>
  <upgradepath endversion="1.4.0.1" moduleid="${moduleId}" startversion="1.4.0.0">
    <steps>
      <!-- Put scripts here to go from 1.4.0.0 to 1.4.0.1 -->
    </steps>
  </upgradepath>

  <upgradepath endversion="1.4.0.2" moduleid="${moduleId}" startversion="1.4.0.1">
	<steps>
	  <!-- Put scripts here to go from 1.4.0.1 to 1.4.0.2 -->
	</steps>
  </upgradepath>

<upgradepath endversion="1.4.0.3" moduleid="${moduleId}" startversion="1.4.0.2">
<steps>
<!--Put scripts here to go from 1.4.0.2 to 1.4.0.3-->
</steps>
</upgradepath>
</paths>
&#13;
&#13;
&#13;

如果您看到所有标签的属性,您会看到它们都按升序重新排列。 startversion 属性现在显示在最后, endversion 属性首先出现。我想要修改XML文件后的属性的原始顺序。我已经尝试了几乎所有的东西并失去了所有的希望有什么方法可以做到这一点吗?还有一种方法可以按降序对属性进行排序吗?它不是正确的解决方案,但它有所帮助。

以下是我用来修改文件的程序的代码段:

&#13;
&#13;
private static void updateXMLFiles(String sStartVersion, String sEndVersion) {
  try {
    for (int c = 0; c < pathsList.size(); c++) {
      File xmlFile = new File(pathsList.get(c).toString());
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder;
      dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(xmlFile);
      doc.getDocumentElement().normalize();
                
      // Get the last <upgradepath> tag in the file.
      // Method Call to verify the version entered and update the XML Files.

      // Write the updated document to the file.
      doc.getDocumentElement().normalize();
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer = transformerFactory.newTransformer();
      DOMSource source = new DOMSource(doc);
      StreamResult result = new StreamResult(new File(pathsList.get(c).toString()));
      transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
      transformer.transform(source, result);
  } 
  catch (SAXException e1) {
    e1.printStackTrace();
  } 
  catch (ParserConfigurationException e1) {
    e1.printStackTrace();
  } 
  catch (IOException e1) {
    e1.printStackTrace();
  } 
  catch (TransformerException e1) {
    e1.printStackTrace();
  }
}

private static void addNewVersion(Document doc, String sStartVersion, String sEndVersion) {
    Element element = doc.getDocumentElement();
    Element upgradePath = doc.createElement("upgradepath");
    upgradePath.setAttribute("startversion", sStartVersion);
    upgradePath.setAttribute("moduleid", "${moduleId}");
    upgradePath.setAttribute("endversion", sEndVersion);
    Element steps = doc.createElement("steps");
    Comment comment = doc.createComment("Put scripts here to go from " + sStartVersion + " to " + sEndVersion);
    steps.appendChild(comment);
    upgradePath.appendChild(steps);
    element.appendChild(upgradePath);
}
&#13;
&#13;
&#13;

有什么方法可以保持属性的顺序完整,或者在最坏的情况下按降序排列? 我的一个朋友建议我试用JAXB,但我找不到实现这个目标的方法。如果有人认为JAXB可以解决这个问题,那就提一下如何格式化现有的XML文件而不是创建一个。 另一个不是主要问题的问题是,尽管我已经使用过 transformer.setOutputProperty(OutputKeys.INDENT,&#34;是&#34;);
新添加的标签没有正确缩进。有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

@XmlType(propOrder = {"startversion", "moduleid", "endversion", "steps"})
public class XmlSubModel {

    private String startversion = "";
    private String moduleid = "";
    private String endversion = "";
    private String steps = "";

    @XmlAttribute
    public String getStartversion() {
        return startversion;
    }

    public void setStartversion(String startversion) {
        this.startversion = startversion;
    }

    @XmlAttribute
    public String getModuleid() {
        return moduleid;
    }

    public void setModuleid(String moduleid) {
        this.moduleid = moduleid;
    }

    @XmlAttribute
    public String getEndversion() {
        return endversion;
    }

    public void setEndversion(String endversion) {
        this.endversion = endversion;
    }

    public String getSteps() {
        return steps;
    }

    public void setSteps(String steps) {
        this.steps = steps;
    }


}