I have a formatted XML file, and I want to convert it to one line string, how can I do that.
Sample xml:
<?xml version="1.0" encoding="UTF-8"?> <books> <book> <title>Basic XML</title> <price>100</price> <qty>5</qty> </book> <book> <title>Basic Java</title> <price>200</price> <qty>15</qty> </book> </books>
Expected output
<?xml version="1.0" encoding="UTF-8"?><books><book> <title>Basic XML</title><price>100</price><qty>5</qty></book><book><title>Basic Java</title><price>200</price><qty>15</qty></book></books>
Thanks in advance.
Document convertStringToDocument(String xmlStr) : This method will take input as String and then convert it to DOM Document and return it. We will use InputSource and StringReader for this conversion. String convertDocumentToString(Document doc) : This method will take input as Document and convert it to String.
In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.
Example Read XML File in Pythonparse() method, to start parsing. Then, we will get the parent tag of the XML file using getroot() . Then we will display the parent tag of the XML file. Now, to get attributes of the sub-tag of the parent tag will use root[0].
//filename is filepath string BufferedReader br = new BufferedReader(new FileReader(new File(filename))); String line; StringBuilder sb = new StringBuilder(); while((line=br.readLine())!= null){ sb.append(line.trim()); }
using StringBuilder is more efficient then concat http://kaioa.com/node/59
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With