0

I am converting a xml file to string using java My xml is

<GTSRequest command="version"> <Authorization account="account" user="user" password="password"/> </GTSRequest> 

and Java Code is

public static void main(String[] args) throws IOException { // TODO code application logic here String a = System.getProperty("user.dir") + "/XML/Get Current GTS Version.xml"; System.out.println(convertXMLFileToString(a)); } public static String convertXMLFileToString(String fileName) { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); InputStream inputStream = new FileInputStream(new File(fileName)); org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); StringWriter stw = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.transform(new DOMSource(doc), new StreamResult(stw)); return stw.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } 

but when i am converting XMl to String my out will change which is i am not want . Output is

<?xml version="1.0" encoding="UTF-8" standalone="no"?><GTSRequest command="version"> <Authorization account="account" password="password" user="user"/> </GTSRequest> 

there is password and user change there place alphabetically
how can i resolve this thanks is advance

1 Answer 1

1

You are transforming the XML to a document and AFTER serializating it...

If you only want the file read the file like a text file:

BufferedReader br = new BufferedReader(new FileReader(fileName)); try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } String everything = sb.toString(); } finally { br.close(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.