Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make XML document from string in java or Android?

I am trying to create a dynamically generated XML file from Java. This is the code that I am trying to use:

try{
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("how many elements: ");
            String str = bf.readLine();
            int no = Integer.parseInt(str);

            System.out.println("enetr root: ");
            String root = bf.readLine();

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document d1 = db.newDocument();
            Element e1 = d1.createElement(root);
            d1.appendChild(e1);

            for (int i = 0; i < no; i++) {
                System.out.println("enter element: ");
                String element = bf.readLine();

                System.out.println("enter data: ");
                String data = bf.readLine();
                Element em = d1.createElement(element);
                em.appendChild(d1.createTextNode(data));

                e1.appendChild(em);
            }
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(d1);

            File file = new File("src\\xml\\copy.xml");
            System.out.println(file);

            if(!file.exists()){
                file.createNewFile();
            }
            OutputStream outputStream = new FileOutputStream(file);
            StreamResult result = new StreamResult(outputStream);
            transformer.transform(source, result);
            outputStream.close();

        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            System.out.println("file not created");
        }

This code works very well. But now I have a string as such:

String xmlRecords = "<data><terminal_id>1000099999</terminal_id><merchant_id>10004444</merchant_id><merchant_info>Mc Donald's - Abdoun</merchant_info></data>";

I want to create a .xml file from this xmlRecords variable. How do I do this?

like image 587
Siten Avatar asked Jul 01 '11 11:07

Siten


People also ask

Can we convert string to XML in Java?

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.

How do you create an XML file and write it in Java?

Steps to create and write XML to a file. Create a Document doc . Create XML elements, attributes, etc., and append to the Document doc . Create a Transformer to write the Document doc to an OutputStream .

What is Android string XML?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.


2 Answers

You can parse an XML string to a Document like this:

String xmlRecords = "<data><terminal_id>1000099999</terminal_id><merchant_id>10004444</merchant_id><merchant_info>Mc Donald's - Abdoun</merchant_info></data>";

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document d1 = builder.parse(new InputSource(new StringReader(xmlRecords)));

You can keep the file writing part mentioned in your question, just replace the creation of the Document instance with above code.

like image 71
wjans Avatar answered Sep 30 '22 04:09

wjans


Here is another solution if you are using XmlPullParser:

XmlPullParser parser = Xml.newPullParser();
parser.setInput(new StringReader("<your><xml><string>Hello</string></xml></your>"));
like image 32
pleerock Avatar answered Sep 30 '22 06:09

pleerock