Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 package de.mfuchs;
00040
00041 import static org.junit.Assert.assertEquals;
00042
00043 import org.junit.Test;
00044
00045 import de.mfuchs.xmlstorage.XMLBool;
00046 import de.mfuchs.xmlstorage.XMLChildrenFilter;
00047 import de.mfuchs.xmlstorage.XMLDoc;
00048 import de.mfuchs.xmlstorage.XMLInt;
00049 import de.mfuchs.xmlstorage.XMLIntRef;
00050 import de.mfuchs.xmlstorage.XMLNode;
00051 import de.mfuchs.xmlstorage.XMLPos;
00052 import de.mfuchs.xmlstorage.stream.XMLStreamReader;
00053 import de.mfuchs.xmlstorage.stream.XMLStreamWriter;
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 public class XMLTest
00064 {
00065
00066
00067
00068
00069 @Test public void test1() throws Exception
00070 {
00071
00072
00073 String path = "packets.xml";
00074
00075 XMLDoc doc = new XMLDoc();
00076
00077 doc.readFile(path);
00078
00079 XMLPos pos = new XMLPos(doc);
00080 pos.goDown("get_packets");
00081
00082
00083
00084 XMLChildrenFilter filter = new XMLChildrenFilter(pos, "packet");
00085
00086 for(XMLNode node: filter) {
00087 XMLIntRef dbId = new XMLIntRef(node, "db_id");
00088
00089 System.out.println("db_id: " + dbId.intValue());
00090
00091 int x = node.getInt("db_id", 12345).intValue();
00092 System.out.println("x: " + x);
00093
00094 System.out.println(node + " \"" + node.get("name") + "\"");
00095 }
00096
00097 pos.back();
00098
00099 doc.writeFile("out.xml");
00100
00101 pos = new XMLPos(doc);
00102
00103
00104
00105
00106 if (pos.go("/get_packets/packet[@db_id=\"7628\"]"))
00107
00108 {
00109
00110 pos.back();
00111 }
00112 }
00113
00114
00115
00116
00117
00118
00119 @Test public void test2Write() throws Exception
00120 {
00121 XMLDoc doc = new XMLDoc();
00122 XMLPos pos = new XMLPos(doc);
00123
00124
00125 pos.create("PARAMS");
00126 pos.create("TEXT");
00127 pos.setContent("Testgruppe");
00128 pos.back();
00129
00130 pos.create("COLOR");
00131 pos.create("RED");
00132 pos.setContent("234");
00133 pos.back();
00134
00135 pos.create("GREEN");
00136 pos.setContent("22");
00137 pos.back();
00138
00139 pos.create("BLUE");
00140 pos.setContent("33");
00141 pos.back();
00142 pos.back();
00143
00144 pos.create("FONT");
00145 pos.create("FONT_FILENAME");
00146 pos.setContent("Arial.ttf");
00147 pos.back();
00148
00149 pos.create("SIZE");
00150 pos.setContent("12");
00151 pos.back();
00152 pos.back();
00153 pos.back();
00154
00155
00156 doc.writeFile("sandra1.xml");
00157 }
00158
00159
00160
00161
00162
00163 @Test public void test2Read() throws Exception
00164 {
00165 XMLDoc doc = new XMLDoc();
00166
00167
00168 doc.readFile("sandra1.xml");
00169
00170 XMLPos pos = new XMLPos(doc);
00171 String filename = "";
00172 int size = 0;
00173
00174
00175 if (pos.go("PARAMS/FONT")) {
00176 if (pos.goDown("FONT_FILENAME")) {
00177 filename = pos.getContent();
00178 pos.back();
00179 }
00180
00181 if (pos.goDown("SIZE")) {
00182 size = new XMLInt(pos.getContent()).intValue();
00183 pos.back();
00184 }
00185
00186 pos.back();
00187 }
00188
00189
00190 System.out.println("font filename: " + filename);
00191 System.out.println("font size: " + size);
00192
00193 assertEquals("Font filename", "Arial.ttf", filename);
00194 assertEquals("Font size", 12, size);
00195 }
00196
00197
00198
00199
00200
00201
00202 @Test public void test3Write() throws Exception
00203 {
00204 XMLDoc doc = new XMLDoc();
00205 XMLPos pos = new XMLPos(doc);
00206
00207
00208 pos.create("root");
00209
00210 pos.create("foo");
00211 pos.setContent("bar");
00212 pos.back();
00213
00214 pos.create("foo2");
00215 pos.put("attr1", "value1");
00216 pos.put("attr2", "value2");
00217
00218 pos.create("foo3");
00219 pos.setContent("bar2");
00220 pos.back();
00221 pos.back();
00222
00223 pos.back();
00224
00225
00226 doc.writeFile("test.xml");
00227 }
00228
00229
00230
00231
00232
00233
00234 @Test public void testStreamingReader1() throws Exception
00235 {
00236 String path = "packets.xml";
00237
00238 XMLStreamReader reader = new XMLStreamReader(path);
00239
00240 reader.goDown("get_packets");
00241
00242 if (reader.firstChild("packet")) do {
00243 XMLInt dbId = new XMLInt(reader, "db_id");
00244 System.out.println("db_id: " + dbId.intValue());
00245
00246 int x = reader.getNode().getInt("db_id", 12345).intValue();
00247 System.out.println("x: " + x);
00248
00249 System.out.println(reader + " \"" + reader.get("name") + "\"");
00250 } while(reader.nextSkipOthers("packet"));
00251
00252 reader.back();
00253
00254 reader.close();
00255 }
00256
00257
00258
00259
00260
00261 @Test public void testStreamingWriter() throws Exception
00262 {
00263 XMLStreamWriter writer = new XMLStreamWriter("stream-example.xml");
00264
00265 writer.create("PERSON");
00266
00267 writer.create("NAME");
00268 writer.setContent("Mustername");
00269 writer.back();
00270
00271 writer.create("VORNAME");
00272 writer.setContent("Mustervorname");
00273 writer.back();
00274
00275 writer.create("ADRESSE");
00276 writer.put("private", new XMLBool(true));
00277 writer.setContent("Musteradresse");
00278 writer.back();
00279
00280 writer.back();
00281
00282 writer.close();
00283 }
00284
00285
00286
00287
00288
00289 @Test public void testStreamingReader2() throws Exception
00290 {
00291 String path = "stream-example.xml";
00292
00293 XMLStreamReader reader = new XMLStreamReader(path);
00294
00295 if (reader.goDown("PERSON")) {
00296 if (reader.goDown("NAME")) {
00297 String content = reader.getContent();
00298 System.out.println("NAME content=" + content);
00299
00300 reader.back();
00301 }
00302
00303 if (reader.goDown("VORNAME")) {
00304 String content = reader.getContent();
00305 System.out.println("VORNAME content=" + content);
00306
00307 reader.back();
00308 }
00309 }
00310
00311 if (reader.goDown("ADRESSE")) {
00312 String priv = reader.get("private");
00313 String content = reader.getContent();
00314 System.out.println("ADRESSE.@private=" + priv);
00315 System.out.println("ADRESSE content=" + content);
00316
00317 reader.back();
00318 }
00319
00320 reader.back();
00321
00322 reader.close();
00323 }
00324
00325
00326
00327
00328
00329
00330
00331 public static void main(String[] args)
00332 {
00333 XMLTest me = new XMLTest();
00334
00335 try {
00336 me.test1();
00337
00338 me.test2Write();
00339 me.test2Read();
00340
00341 me.test3Write();
00342
00343 me.testStreamingReader1();
00344 me.testStreamingReader2();
00345 } catch(Exception e) {
00346 e.printStackTrace();
00347 }
00348 }
00349 }