From JSON to XML and back in Java -


Changing XML to JSON is quite straight forward. XML attributes are created as string values ​​and XML elements JSON objects are created. Naming Connection is strictly for XML from JSON. The way back is more complicated if it is working in Java, then there is a way to firmly convert formats?

When you make a bean, two libraries make your life easier:

  • For JSON
  • For XML

    Use the official format conversion between Bean JSON and XML using this example as a simple reference Do:

      Import java.io.biteArrayOutputStream; Import java.io.file; Import java.io.FileInputStream; Import java.io.FileOutputStream; Import java.io.IOException; Import java.io.InputStream; Import java.io.InputStreamReader; Import java.io.OutputStream; Import java.io.printWriter; Import javax.xml.bind.JAXBContext; Import javax.xml.bind.Marshaller; Import javax.xml.bind.Unmarshaller; Import javax.xml.bind.annotation.XmlRootElement; Import com.google.gson.son; Import com.google.gson.Gsonbuilder; @ XMLRealElement (name = "ft") public class results {public final fixed string XML_FILE = "fruit.xml"; Public last fixed string JSON_FILE = "fruit.json"; Jason (inputstream in) from public static fruits {GSON gsn = new jeansbilder (). Create (); Fruit Result = GSN.fr Jasmine (new interstitial reader (in), fruit.class); Return result; } XML (InputStreamIn) throws an exception from public static fruits {JAXBContext reference = JAXBContext.newInstance (fruit class); Unmarshaller um = context.createUnmarshaller (); Return (fruits) um.unmarshal (in); } Public static zero principal (string [] args throws exceptions (fruit = new fruit ("apple", "red", "sweet"); Fruits f2 = new fruit ("warrior", "white", "doo"); Println (f.toXML ()); Println (f2.toJSON ()); F.saveXML (new FileOutputStream (new file (XML_FILE)); F2.saveJSON (new FileOutputStream (new file (JSON_FILE)); Fruits F3 = fruits.frame XML (new file inputstream (new file (XML_FILE)); Println (f3.toJSON ()); Fruit F4 = Fruit.Frame Jason (new fileInststream (new file (JSONIFELE)); Println (f4.toXML ()); } Name of the private string; Private string color; Personal string taste; Public Fruit () {// Default Constructor} Public Fruit (Last String Name, Last String Color, Last String Taste) {this.name = name; This.color = color; This.taste = taste; } / ** * Color return * / Public final string getColor () {this.color return; } / ** * name * / public return the final string getName () {this.name; } / ** * Taste * Return * / Public Last String GetTaste () {this.taste return; } Public Zero save throws JSON (Output Stream Out) IOException {GsonBuilder GB = new GsonBuilder (); Gb.setPrettyPrinting (); Gb.disableHtmlEscaping (); Gsan gsn = gb.kreate (); Printer author = new printer (out); Gson.toJson (this, author); Writer.flush (); Writer.close (); } Public Zero saving XML (OutputStream Out) throws exceptions {JAXBContext References = JAXBContext.newInstance (F. Category); Marshaller M = Reference. Cretemars (); M.setProperty (Marshall JAXB_FORMATTED_OUTPUT, boolean. TRUE); M. Marshall (this, outside); } / ** * color @ to set the ultimate color * / public final zero set collar (string color) {this.color = color; } / ** * @not name * to set name * / public last zero setname (string name) {this.name = name; } / ** * tastes * to set the ultimate flavor * / public final zero set (string taste) {this.taste = taste; } Public string toJSON () throws IOException {GsonBuilder gb = new GsonBuilder (); Gb.setPrettyPrinting (); gb.disableHtmlEscaping (); Gsan gsn = gb.kreate (); Return gson.toJson (this, fruit. Square); } Public string toxml (exception) {out byteArrayOutputStream = new ByteArrayOutputStream (throws); JAXBContext References = JAXBContext.newInstance (fruit class); Marshaller M = Reference. Cretemars (); M.setProperty (Marshall JAXB_FORMATTED_OUTPUT, boolean. TRUE); M. Marshall (this, outside); Return out.trusting (); }}    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -