0

I have searched several websites and all of them give different examples of how to write to a XML file. This is how the XML should look like after I write into it:

 <?xml version= "1.0"?>
  <Customers>
<Marker category="Production">
<title lang="en">Invensys</title>
<site_location>Neponset Avenue 22, Foxborough, Foxboro, MA, United States</site_location>
<latitude>42.066817</latitude>
<longitude>-71.24814</longitude>
<site_status>Normal</site_status>  
</Marker>
<Marker category="Production">
<title lang="en">Yokogawa Japan</title>
<site_location>Shinjuku, Tokyo, Japan</site_location>
<latitude>36.543915</latitude>
<longitude>136.629281</longitude>
<site_status>Critical</site_status>  
</Marker>
    </Customers>

I know there are many API's for this so please help me with a basic writing method for a creation of a simpler XML file and I will continue from there.

Thanks in advance.

David Faizulaev
  • 4,651
  • 21
  • 74
  • 124
  • Your method will highly depend on your implementation. As you state, there are MANY different APIs, and they all tend to be different. However, in my experience, usually you construct a "Document", then start adding "Nodes" to it to build the DOM. When you are done, there is usually some method to write out the document object you created. – CodeChimp Dec 05 '13 at 12:20
  • If your xml structure remains same use JAXB. a 5 minutes Job. there are lots of online sites where you can generate xsd from xml file and using xsd generate Java classes you have JAXB classes from schema code generator in eclipse and finally refer http://www.mkyong.com/java/jaxb-hello-world-example/ tutorial to get convert to/from file – Karthik Prasad Dec 05 '13 at 12:26

2 Answers2

2

Java certainly has APIs for writing XML. Start with a Document:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();

//Create the root Customers element
Element rootElement = doc.createElement("Customers");
doc.appendChild(rootElement);

//Create Marker element
Element markerElement = doc.createElement("Marker");
markerElement.setAttribute("category","Production");
rootElement.appendChild(markerElement);

and so on.

A nice little tutorial can be found here

This is just one way of many, but it's probably the best one to get started with.

NickJ
  • 9,380
  • 9
  • 51
  • 74
  • Nick thanks a lot !!! This is the best one i have seen all day. BTW, after i finish writing and close the file, is it automatically saved as XML ? – David Faizulaev Dec 05 '13 at 12:26
  • No problem. If your XML is big and complex this way gets cumbersome. Another thing to check out is JAXB: http://www.mkyong.com/java/jaxb-hello-world-example/ – NickJ Dec 05 '13 at 12:28
0

Well, there's a whole host of options here. you could write the xml as a string, then just output it to a file, thats the quick dirty solution. You could create a DOM object and then write the string representation of it into a text file. You could create JAXB objects, and use them to create the string representation.

At the end of the day, whichever you use, all you're doing is writing text to a file that ends in .xml

M21B8
  • 1,867
  • 10
  • 20