1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 26 import nu.xom.Builder; 27 import nu.xom.Comment; 28 import nu.xom.Document; 29 import nu.xom.Node; 30 import nu.xom.ParentNode; 31 import nu.xom.ParsingException; 32 import nu.xom.ProcessingInstruction; 33 34 44 public class Restructurer { 45 46 47 public static void processNode(Node current) { 51 52 if (current instanceof Comment 53 || current instanceof ProcessingInstruction) { 54 Document document = current.getDocument(); 55 ParentNode root = document.getRootElement(); 56 current.detach(); 57 document.insertChild(current, document.indexOf(root)); 58 } 59 else { 60 for (int i = 0; i < current.getChildCount(); i++) { 61 processNode(current.getChild(i)); 62 } 63 } 64 65 } 66 67 68 public static void main(String [] args) { 69 70 if (args.length <= 0) { 71 System.out.println( 72 "Usage: java nu.xom.samples.RestructureDriver URL" 73 ); 74 return; 75 } 76 String url = args[0]; 77 78 try { 79 Builder parser = new Builder(); 81 82 Document document = parser.build(url); 84 85 Restructurer.processNode(document.getRootElement()); 87 88 System.out.println(document.toXML()); 90 91 } 92 catch (ParsingException ex) { 93 System.out.println(url + " is not well-formed."); 94 } 95 catch (IOException ex) { 96 System.out.println( 97 "Due to an IOException, the parser could not read " + url 98 ); 99 } 100 101 } 102 103 } 104 105 106 | Popular Tags |