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.examples.core.pojo.PojoComplex; 017import org.apache.juneau.xml.XmlSerializer; 018 019import java.util.HashMap; 020import java.util.List; 021 022/** 023 * Xml configuration example. 024 * 025 * <ul class='seealso'> 026 * <li class='extlink'>{@source} 027 * </ul> 028 */ 029public class XmlConfigurationExample { 030 031 /** 032 * Examples on XML Serializers configured using properties 033 * defined in XmlSerializer class. 034 * 035 * @param args Unused. 036 * @throws Exception Unused. 037 */ 038 public static void main(String[] args) throws Exception { 039 040 Pojo aPojo = new Pojo("a","<pojo>"); 041 042 /** 043 * Xml Serializers can be configured using properties defined in XmlSerializer. 044 * Produces 045 * <object> 046 * <name><pojo></name> 047 * <id>a</id> 048 * </object> 049 */ 050 String withWhitespace = XmlSerializer.create().ws().build().serialize(aPojo); 051 // the output will be padded with spaces after format characters. 052 System.out.println(withWhitespace); 053 054 HashMap<String, List<Pojo>> values = new HashMap<>(); 055 PojoComplex pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values); 056 057 //Produces 058 //<object><innerPojo><name>name0</name><id>1.0</id></innerPojo><id>pojo</id></object> 059 String mapescaped = XmlSerializer.create().trimEmptyMaps(true).build().serialize(pojoc); 060 // the output will have trimmed Empty maps. 061 System.out.println(mapescaped); 062 063 //Produces 064 //<object xmlns="http://www.apache.org/2013/Juneau"><name><pojo></name><id>a</id></object> 065 String nspaceToRoot = XmlSerializer.create().ns().addNamespaceUrisToRoot(true).build().serialize(aPojo); 066 // the output will add default name space to the xml document root. 067 System.out.println(nspaceToRoot); 068 069 Pojo nPojo = new Pojo("a",null); 070 071 //Produces 072 //<object><id>a</id></object> 073 String nullescaped = XmlSerializer.create().trimNullProperties(true).build().serialize(nPojo); 074 // the output will have trimmed null properties. 075 System.out.println(nullescaped); 076 077 //Produces 078 //<object xmlns="http://www.pierobon.org/iis/review1.htm.html#one"><name><pojo></name><id>a</id></object> 079 String dNamsSpace = XmlSerializer.create().enableNamespaces(true).defaultNamespace("http://www.pierobon.org" + 080 "/iis/review1.htm.html#one").autoDetectNamespaces(true).addNamespaceUrisToRoot(true).build() 081 .serialize(aPojo); 082 // the output will have new default namespace added. 083 System.out.println(dNamsSpace); 084 085 086 } 087}