Results 1 to 1 of 1
  1. #1
    BeMathis is offline Member
    Join Date
    Oct 2009
    Rep Power

    Default Parse and output XML document while preserving attribute order

    QUESTION: How can I take in an element with attributes from an XML and output the same element and attributes while preserving the order of those attributes?

    The following code will parse and XML document and generate (practically) unchanged output. However, all attributes are ordered a-z
    Example: The following element
    <work_item_type work_item_db_site="0000000000000000" work_item_db_id="0" work_item_type_code="3" user_tag_ident="Step" name="Work Step" gmt_last_updated="2008-12-31T18:00:00.000000000" last_upd_db_site="0000000000000000" last_upd_db_id="0" rstat_type_code="1">

    is output as:
    <work_item_type gmt_last_updated="2008-12-31T18:00:00.000000000" last_upd_db_id="0" last_upd_db_site="0000000000000000" name="Work Step" rstat_type_code="1" user_tag_ident="Step" work_item_db_id="0" work_item_db_site="0000000000000000" work_item_type_code="3">

    As you may notice, there is no difference in these besides order of the attributes!

    Please, someone help me out with this! I have a feeling the solution is simple :D

    The following code take source.xml and outputs it to a new file DEST_filename

    Java Code:
     private void OutputFile(String DEST_filename, String style_filename){
    	//StreamSource stylesheet = new StreamSource(style_filename);
    		File dest_file = new File(DEST_filename);
    		TransformerFactory tranFactory = TransformerFactory.newInstance();
    		Transformer aTransformer = tranFactory.newTransformer();
    		aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    		Source src = new DOMSource("source.xml");
    		Result dest = new StreamResult(dest_file);
    		aTransformer.transform(src, dest);
    	catch(Exception e){
    Last edited by BeMathis; 10-14-2009 at 06:59 PM.

Similar Threads

  1. Preserving web application configuration
    By Andrey in forum Web Frameworks
    Replies: 1
    Last Post: 10-24-2009, 02:30 AM
  2. how to read an attribute of an xml tag in jsp.
    By himacherla in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-21-2009, 06:27 AM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  4. how to add an attribute with sax?
    By cecily in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 04:09 AM
  5. Problem with Attribute in JSP
    By Albert in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-13-2007, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts