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.commons.logging.*; 020import org.apache.juneau.examples.core.pojo.*; 021import org.apache.juneau.marshall.xml.*; 022 023/** 024 * Sample class which shows the simple usage of XmlSerializer. 025 * 026 */ 027public class XmlSimpleExample { 028 029 /** 030 * Serializing SimplePojo bean into human readable XML 031 * and Deserialize back to Pojo instance type. 032 * 033 * @param args Unused. 034 * @throws Exception Unused. 035 */ 036 public static void main(String[] args) throws Exception { 037 038 // Fill some data to a Pojo bean 039 var pojo = new Pojo("id", "name"); 040 041 // Serialize to human readable XML and print 042 /** 043 * <object> 044 * <name>name</name> 045 * <id>id</id> 046 * </object> 047 */ 048 var serial = XmlSerializer.DEFAULT_SQ_READABLE.write(pojo); 049 RichLogger.getLogger(XmlSimpleExample.class).info(serial); 050 051 // Deserialize back to Pojo instance 052 var obj = XmlParser.DEFAULT.read(serial, Pojo.class); 053 054 assert obj.getId().equals(pojo.getId()); 055 assert obj.getName().equals(pojo.getName()); 056 057 // The object above can be parsed thanks to the @BeanCtor annotation on PojoComplex 058 // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them. 059 060 } 061}