KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > Linkset


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24
25 import java.io.IOException JavaDoc;
26
27 import nu.xom.Builder;
28 import nu.xom.Document;
29 import nu.xom.Element;
30 import nu.xom.Elements;
31 import nu.xom.ParsingException;
32
33
34 /**
35  *
36  * <p>
37  * Demonstrates extracting elements from one document
38  * and inserting them into another document.
39  * </p>
40  *
41  * @author Elliotte Rusty Harold
42  * @version 1.0
43  *
44  */

45 public class Linkset {
46   
47   public static void main(String JavaDoc[] args) {
48     
49     String JavaDoc url = "http://www.slashdot.org/slashdot.rdf";
50     
51     try {
52       Builder parser = new Builder();
53       
54       // Parse the document
55
Document document = parser.build(url);
56       Element oldRoot = document.getRootElement();
57       Element newRoot = new Element("linkset");
58       Elements toplevel = oldRoot.getChildElements();
59       for (int i = 0; i < toplevel.size(); i++) {
60         Element element = toplevel.get(i);
61         Element link = element.getFirstChildElement("link",
62           "http://my.netscape.com/rdf/simple/0.9/");
63         link.detach();
64         newRoot.appendChild(link);
65       }
66       System.out.println(newRoot.toXML());
67     }
68     catch (ParsingException ex) {
69       System.out.println(url + " is not well-formed.");
70     }
71     catch (IOException JavaDoc ex) {
72       System.out.println(
73        "Due to an IOException, the parser could not read " + url
74       );
75     }
76      
77   } // end main
78

79 }
80
81
Popular Tags