@@ -302,79 +302,6 @@ System.out.println(builder.toXml());
302302</root >
303303```
304304
305- ``` java
306- String inventory =
307- " {\n "
308- + " \" inventory\" : {\n "
309- + " \" #comment\" : \" Test is test comment\" ,\n "
310- + " \" book\" : [\n "
311- + " {\n "
312- + " \" -year\" : \" 2000\" ,\n "
313- + " \" title\" : \" Snow Crash\" ,\n "
314- + " \" author\" : \" Neal Stephenson\" ,\n "
315- + " \" publisher\" : \" Spectra\" ,\n "
316- + " \" isbn\" : \" 0553380958\" ,\n "
317- + " \" price\" : \" 14.95\"\n "
318- + " },\n "
319- + " {\n "
320- + " \" -year\" : \" 2005\" ,\n "
321- + " \" title\" : \" Burning Tower\" ,\n "
322- + " \" author\" : [\n "
323- + " \" Larry Niven\" ,\n "
324- + " \" Jerry Pournelle\"\n "
325- + " ],\n "
326- + " \" publisher\" : \" Pocket\" ,\n "
327- + " \" isbn\" : \" 0743416910\" ,\n "
328- + " \" price\" : \" 5.99\"\n "
329- + " },\n "
330- + " {\n "
331- + " \" -year\" : \" 1995\" ,\n "
332- + " \" title\" : \" Zodiac\" ,\n "
333- + " \" author\" : \" Neal Stephenson\" ,\n "
334- + " \" publisher\" : \" Spectra\" ,\n "
335- + " \" isbn\" : \" 0553573862\" ,\n "
336- + " \" price\" : \" 7.50\"\n "
337- + " }\n "
338- + " ]\n "
339- + " }\n "
340- + " }" ;
341- String title = U . selectToken(U . fromJsonMap(inventory), " //book[@year>2001]/title/text()" );
342- // "Burning Tower"
343-
344- String json =
345- " {\n "
346- + " \" Stores\" : [\n "
347- + " \" Lambton Quay\" ,\n "
348- + " \" Willis Street\"\n "
349- + " ],\n "
350- + " \" Manufacturers\" : [\n "
351- + " {\n "
352- + " \" Name\" : \" Acme Co\" ,\n "
353- + " \" Products\" : [\n "
354- + " {\n "
355- + " \" Name\" : \" Anvil\" ,\n "
356- + " \" Price\" : 50\n "
357- + " }\n "
358- + " ]\n "
359- + " },\n "
360- + " {\n "
361- + " \" Name\" : \" Contoso\" ,\n "
362- + " \" Products\" : [\n "
363- + " {\n "
364- + " \" Name\" : \" Elbow Grease\" ,\n "
365- + " \" Price\" : 99.95\n "
366- + " },\n "
367- + " {\n "
368- + " \" Name\" : \" Headlight Fluid\" ,\n "
369- + " \" Price\" : 4\n "
370- + " }\n "
371- + " ]\n "
372- + " }\n "
373- + " ]\n "
374- + " }" ;
375- List<String > names = U . selectTokens(U . fromJsonMap(json), " //Products[Price>=50]/Name/text()" );
376- // [Anvil, Elbow Grease]
377- ```
378305Simplify XML document creation by structuring your code like the final document.
379306
380307This code:
@@ -389,9 +316,11 @@ XmlBuilder builder = XmlBuilder.create("Projects")
389316 .e(" JetS3t" ). a(" language" , " Java" ). a(" scm" , " CVS" )
390317 .e(" Location" ). a(" type" , " URL" )
391318 .t(" https://jets3t.s3.amazonaws.com/index.html" );
319+ System . out. println(builder. toXml(Xml . XmlStringBuilder . Step . TWO_SPACES ));
320+ System . out. println(builder. toJson(Json . JsonStringBuilder . Step . TWO_SPACES ));
392321```
393322
394- Generates the following XML document :
323+ Generates the following XML and JSON documents :
395324
396325``` xml
397326<?xml version =" 1.0" encoding =" UTF-8" ?>
@@ -404,6 +333,28 @@ Generates the following XML document:
404333 </JetS3t >
405334</Projects >
406335```
336+ ``` json
337+ {
338+ "Projects" : {
339+ "underscore-java" : {
340+ "-language" : " Java" ,
341+ "-scm" : " SVN" ,
342+ "Location" : {
343+ "-type" : " URL" ,
344+ "#text" : " https://github.com/javadev/underscore-java/"
345+ }
346+ },
347+ "JetS3t" : {
348+ "-language" : " Java" ,
349+ "-scm" : " CVS" ,
350+ "Location" : {
351+ "-type" : " URL" ,
352+ "#text" : " https://jets3t.s3.amazonaws.com/index.html"
353+ }
354+ }
355+ }
356+ }
357+ ```
407358
408359Underscore-java is a java port of [ Underscore.js] ( https://underscorejs.org/ ) .
409360
0 commit comments