Srinivasan
Srinivasan

Reputation: 12060

XML Data is sorted

I am constructing XML code using Java. See my code snippet.

    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);
        }

The problem is all the attributes are sorted automatically. How to restrict it?

For eg,

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

but i want

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

Please suggest some idea.

Thanks,

Upvotes: 0

Views: 319

Answers (2)

Tim
Tim

Reputation: 20777

Duplicate: Order of XML attributes after DOM processing

From 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.

Credit to Robert Rossney

Upvotes: 4

Brian Agnew
Brian Agnew

Reputation: 272417

XML attributes are not ordered. How they're output is dependent on the XML output mechanism you use.

Consequently you could write your on output mechanism, but you shouldn't rely on any consumer to consume them in an ordered fashion. If you want/need ordering, you should instead specify a sequence of XML elements below this node.

Upvotes: 0

Related Questions