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