001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.examples.core.xml;
014
015import org.apache.juneau.examples.core.pojo.Pojo;
016import org.apache.juneau.xml.XmlParser;
017import org.apache.juneau.xml.XmlSerializer;
018
019/**
020 * Sample class which shows the simple usage of XmlSerializer.
021 *
022 * <ul class='seealso'>
023 *    <li class='extlink'>{@source}
024 * </ul>
025 */
026public class XmlSimpleExample {
027
028   /**
029    * Serializing SimplePojo bean into human readable XML
030    * and Deserialize back to Pojo instance type.
031    *
032    * @param args Unused.
033    * @throws Exception Unused.
034    */
035   public static void main(String[] args) throws Exception {
036
037      // Fill some data to a Pojo bean
038      Pojo pojo = new Pojo("id","name");
039
040      // Serialize to human readable XML and print
041      /**
042       * <object>
043       * <name>name</name>
044       * <id>id</id>
045       * </object>
046       */
047      String serial = XmlSerializer.DEFAULT_SQ_READABLE.serialize(pojo);
048      System.out.println(serial);
049
050      // Deserialize back to Pojo instance
051      Pojo obj = XmlParser.DEFAULT.parse(serial, Pojo.class);
052
053      assert obj.getId().equals(pojo.getId());
054      assert obj.getName().equals(pojo.getName());
055
056      // The object above can be parsed thanks to the @Beanc annotation on PojoComplex
057      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
058
059   }
060}