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