2009-10-20 22 views
0

我正在構建使用Java的XML代碼。看我的代碼片段。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"/> 

請提出一些想法。

感謝,

回答

0

XML屬性訂購。它們的輸出方式取決於您使用的XML輸出機制。因此,你可以編寫你的輸出機制,但你不應該依賴任何消費者以有序的方式來使用它們。如果您想要/需要排序,則應該在此節點下指定一個XML元素序列。

4

重複:Order of XML attributes after DOM processing

the accepted answer

看看XML 推薦的第3.1節。它說:「請注意 屬性規範 在起始標籤或空元素標籤中的順序是 不重要。」

如果一個軟件,需要以特定的順序出現 一個XML元素 屬性,即軟件是 不處理XML,它的處理 文字,看起來像表面上XML 。它需要被修復。

如果它不能是固定的,你必須符合其 要求 產生的文件,你不能可靠地使用 標準的XML工具來產生這些 文件。

貸記Robert Rossney