Changeset 3970
- Timestamp:
- 2003-03-25T09:44:12+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/util/XMLConverter.java
r3941 r3970 106 106 { 107 107 outputEscaping = true; 108 return getString(xmlNode, 0); 109 } 110 111 112 private String getString(Node xmlNode, int depth) 108 return getString(xmlNode, 0, false); 109 } 110 111 /** returns the node as a nicely formatted String - this introduces extra 112 * text nodes if the String is read back in as a DOM, so should only be 113 * used for printing */ 114 public String getPrettyString(Node xmlNode) { 115 116 outputEscaping = true; 117 return getString(xmlNode, 0, true); 118 } 119 120 private String getString(Node xmlNode, int depth, boolean pretty) 113 121 { 114 122 String xmlRepresentation = ""; … … 122 130 // Handle Element nodes 123 131 if (nodeType == Node.ELEMENT_NODE) { 124 xmlRepresentation += "\n"; 125 for (int i = 0; i < depth; i++) 126 xmlRepresentation += " "; 132 if (pretty) { 133 xmlRepresentation += "\n"; 134 for (int i = 0; i < depth; i++) { 135 xmlRepresentation += " "; 136 } 137 } 127 138 128 139 // Write opening tag 129 140 xmlRepresentation += "<" + nodeName; 130 141 131 142 // Write the node attributes 132 143 NamedNodeMap nodeAttributes = xmlNode.getAttributes(); … … 148 159 return xmlRepresentation; 149 160 } 150 161 151 162 // Close the opening tag 152 163 xmlRepresentation += ">"; 153 164 154 165 // Apply recursively to the children of this node 155 166 NodeList children = xmlNode.getChildNodes(); 156 167 for (int i = 0; i < children.getLength(); i++) { 157 xmlRepresentation += getString(children.item(i), depth + 1 );158 } 159 168 xmlRepresentation += getString(children.item(i), depth + 1, pretty); 169 } 170 160 171 // Write closing tag 161 if (xmlRepresentation.endsWith("\n")) { 162 for (int i = 0; i < depth; i++) 163 xmlRepresentation += " "; 164 } 165 xmlRepresentation += "</" + nodeName + ">\n"; 166 } 167 172 if (pretty) { 173 if (xmlRepresentation.endsWith("\n")) { 174 for (int i = 0; i < depth; i++) 175 xmlRepresentation += " "; 176 } 177 } 178 xmlRepresentation += "</" + nodeName + ">"; 179 if (pretty) { 180 xmlRepresentation += "\n"; 181 } 182 } 183 168 184 // Handle Text nodes 169 185 else if (nodeType == Node.TEXT_NODE) {
Note:
See TracChangeset
for help on using the changeset viewer.