1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 26 import nu.xom.Attribute; 27 import nu.xom.Comment; 28 import nu.xom.Document; 29 import nu.xom.Element; 30 import nu.xom.Serializer; 31 32 43 public class HelloXMLBase { 44 45 public static void main(String [] args) { 46 47 Document doc; 48 String base1 = "http://www.base1.com"; 49 String base2 = "http://www.base2.com"; 50 String base3 = "base3.html"; 51 52 Element root = new Element("root"); 53 doc = new Document(root); 54 doc.setBaseURI(base1); 55 Element child = new Element("child"); 56 root.appendChild(child); 57 child.setBaseURI(base2); 58 child.appendChild(new Comment("here I am")); 59 60 Element child2 = new Element("child2"); 61 root.appendChild(child2); 62 63 Element child3 = new Element("child3"); 64 root.appendChild(child3); 65 child3.addAttribute(new Attribute("xml:base", 66 "http://www.w3.org/XML/1998/namespace", base2)); 67 68 Element child4 = new Element("child4"); 69 root.appendChild(child4); 70 child4.addAttribute(new Attribute("xml:base", 71 "http://www.w3.org/XML/1998/namespace", base3)); 72 73 try { 74 Serializer serializer 75 = new Serializer(System.out, "ISO-8859-1"); 76 serializer.setPreserveBaseURI(true); 77 serializer.write(doc); 78 serializer.flush(); 79 serializer.setPreserveBaseURI(false); 80 serializer.write(doc); 81 serializer.flush(); 82 } 83 catch (IOException ex) { 84 ex.printStackTrace(); 86 } 87 88 } 89 90 } 91 | Popular Tags |