java - XML 数据已排序

标签 java

我正在使用 Java 构建 XML 代码。请参阅我的代码片段。

    Document document = null;
    String xml = "";
    ReportsDAO objReportsDAO = null;
    try 
    {
        logger.info("Getting XML data for Consumable Report Starts...");
        objReportsDAO = new ReportsDAO();

        List consumableDTOLst = objReportsDAO.getConsumableData(issuedBy, issuedTo, employeeType, itemCode, itemName, className, transactionFromDate, transactionToDate, machineCode, workOrderNumber, jobName, customerId);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.newDocument();
        Element rootElmnt = (Element) document.createElement("items");  
        document.appendChild(rootElmnt);

        Element elmt = null;
        ConsumableDTO objConsumableDTO = null;
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

        for (int i = 0; i < consumableDTOLst.size(); i++) 
        {
            objConsumableDTO = (ConsumableDTO)consumableDTOLst.get(i);
            elmt =  (Element) document.createElement("item");
            elmt.setAttribute("IssuedBy", objConsumableDTO.getIssuedBy());
            elmt.setAttribute("IssuedTo", objConsumableDTO.getIssuedTo());
            elmt.setAttribute("EMPLOYECADRE", objConsumableDTO.getEmployeeType());
            elmt.setAttribute("ITEMCODE", objConsumableDTO.getItemCode());
            elmt.setAttribute("ITEMNAME", objConsumableDTO.getItemName());
            elmt.setAttribute("ITEMCLASS", objConsumableDTO.getClassName());
            elmt.setAttribute("DATE", sdf.format(objConsumableDTO.getTransactionDate()));
            elmt.setAttribute("machineCode", objConsumableDTO.getMachineCode());
            elmt.setAttribute("JOB", objConsumableDTO.getJobName());
            elmt.setAttribute("WORKORDERNUMBER", objConsumableDTO.getWorkOrderNumber());
            elmt.setAttribute("CustomerName", objConsumableDTO.getCustomerName());
            elmt.setAttribute("RoleName", objConsumableDTO.getGroupName());
            elmt.setAttribute("VendorName", objConsumableDTO.getVendorName());
            elmt.setAttribute("QTY", String.valueOf(Math.abs(objConsumableDTO.getQuantity())));
            elmt.setAttribute("unitDescription", objConsumableDTO.getUnitDescription());
            elmt.setAttribute("RATEPERQTY", String.valueOf(objConsumableDTO.getRate()));
            elmt.setAttribute("AMOUNT", String.valueOf(objConsumableDTO.getAmount()));
            rootElmnt.appendChild(elmt);
        }

问题是所有属性都会自动排序。如何限制?

例如,

<empdetails age="25" name="john"/>

但我想要

<empdetails name="john" age="25"/>

请提出一些想法。

谢谢

最佳答案

重复:Order of XML attributes after DOM processing

来自the accepted answer :

Look at section 3.1 of the XML recommendation. It says, "Note that the order of attribute specifications in a start-tag or empty-element tag is not significant."

If a piece of software requires attributes on an XML element to appear in a specific order, that software is not processing XML, it's processing text that looks superficially like XML. It needs to be fixed.

If it can't be fixed, and you have to produce files that conform to its requirements, you can't reliably use standard XML tools to produce those files.

归功于Robert Rossney

关于java - XML 数据已排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1594400/

相关文章:

java - 有没有办法检测电子邮件地址是否属于现有帐户?

java - java中的日期格式

Java : Io exception: The Network Adapter could not establish the connection

java - Java中的RSA加密算法 : no BigIntegers

java - Java 中的 XML 模板

java - 异常最终被吞噬

java - 通过可点击的 ListView 将 firebase 数据从一个 Activity 传递到另一个 Activity

java - 如何执行onDestroy中的方法?

java - 如何在 IntelliJ Idea 中为生成的单元测试添加默认导入

java - 如何使用 JMH 对异步操作进行基准测试