Jeffrey Friesen

Author
+ Follow
since Dec 29, 2012
Jeffrey likes ...
Android Java
I'm a freelance tutor, author, and software developer with an emphasis on Java and Android. I've written numerous books and articles for Apress (www.apress.com), JavaWorld (www.javaworld.com), informIT (www.informit.com), java.net, DevSource (www.devsource.com), SitePoint (www.sitepoint.com), BuildMobile (www.buildmobile.com), and JSPro (www.jspro.com).
Canada
Cows and Likes
Cows
Total received
5
In last 30 days
0
Total given
0
Likes
Total received
13
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jeffrey Friesen

Hello everyone,

I enjoyed my time with you. I hope those who won will enjoy my book. If you have any questions about any book content, you can always contact me via my website: javajeff.ca

All the best.

Jeff Friesen
Hi Billy,

The book does not focus on performance. If there is another edition, performance would be covered. Meanwhile, you might want to check out blog posts such as

Parsing a large JSON file efficiently and easily: https://www.ngdata.com/parsing-a-large-json-file-efficiently-and-easily/

Jeff
Thanks everyone for your kind words.

Jeff
Hi Paul,

I think I can finally solve your problem. First, consider the following updated XML.java source code (I hope I get the formatting right):



This program works with the file.xml that I previously presented. For completeness, here it is again:



When I run this program under JDK 8u60, I observe the following output:



The first output line proves that the doc variable is not assigned the null reference.

The second output line shows the result of calling Document's toString() method on the reference stored in doc. This output shows that doc is referencing a Document node. This is proven by #document, which is the name of the node (as returned by Node's getNodeName() method -- Document extends Node). The null value that you are seeing after the : character is the node's value (as returned by Node's getNodeValue() method). The Document node has no value, which is verified by the third line -- doc.getNodeValue() = null.

The fourth line proves that the element variable is not assigned a null reference. Its name is adminappv and its value is null. Here is where confusion starts.

It would seem that Element element = (Element) doc.getElementsByTagName("adminappv").item(0); might return the 0 text between the <adminappv> and </adminappv> tags, but this is not the case. The text between these tags is stored in a #text node that's located (in the DOM tree) as a child of the adminappv element node. You conveniently access this child by calling the element node's getFirstChild() method. You could also use getChildNodes() and NodeList's item() method with a 0 argument, as in System.out.printf("element.getChildNodes().item(0) = %s%n", element.getChildNodes().item(0));, but that's more cumbersome.

The final line roughly repeats the output of the previous line. However, the corresponding code is a bit different to show you an alternative.

You might want to check out <a href="https://www.mathworks.com/matlabcentral/answers/260969-why-do-i-receive-an-empty-document-document-null-when-i-read-a-xml-file-with-xmlread">why do I receive an empty document [#document: null] when I read a xml file with xmlread?</a> for more information.

Jeff



Hi Paul,

I've inserted the following line in the program that I presented to you earlier:

System.out.println(doc == null);

This line appears immediately after the following line:

doc = factory.newDocumentBuilder().parse(fis);

When I run the program (under JDK 8u60), I observe false being output for the System.out.println() call. This proves that doc is not assigned the null reference. Instead, it is assigned a reference to a nonnull object containing the text I showed earlier.

Jeff
Hi Celinio,

There are several new features in the second edition:

1) Chapter 2 shows the proper way to obtain an XML reader. The previous edition's approach is deprecated.
2) Chapter 3 also introduces the DOM's Load and Save, Range, and Traversal APIs.
3) Chapter 6 shows how to work with SAXON to move beyond XSLT/XPath 1.0.
4) Chapter 11 is a new (lengthy) chapter that explores Jackson.
5) Chapter 12 is a new (lengthy) chapter that explores JSON-P.

Also, several minor corrections to previous edition content have been made, various figures have been updated, and numerous new exercises have been added.

Jeff
Hi Paul,

I'm assuming that you mean that the null reference is being assigned to the doc variable. The output shows that a non-null object is being assigned to doc whose toString() method returns "[#document: null]". Maybe I'm missing something and you're referring to the null in

document: null

and not to a null reference being assigned to doc. Please clarify. Thanks.

Jeff
Hi Yagiz,

JSON and XML nicely complement each other and are widely used. Therefore, it makes sense to be able to validate either kind of document, and so I would say that JSON Schema makes as much sense as XML Schema.

Jeff
Hi Randy,

I think that XML and JSON (and YAML) will be around for a long time. As I've said in a previous post, XML and JSON nicely complement each other, and various tools are making use of this. They are here to stay.

I hope that Oracle will finally officially support JSON in Java SE (as they do in Java EE via JSON-P).

Jeff
Hi Celinio,

My book covers namespaces in Chapter 1. I devote just over four pages to the topic. I discuss what a namespace is and explore namespace prefixes. Later in the chapter, I talk about namespaces again in the context of XML Schema.

Jeff
Hi Wayne,

JSON is minimal in comparison to XML. You could easily cover it in an hour (perhaps two). Although XML is bigger than JSON, you should be able to cover the language features in a couple of hours. It may take a bit longer to cover validation in terms of Document Type Definitions and schemas. If you're interested in covering parsing, XML transformations, the Document Object Model, and so forth, expect the course to run over a few more days.

When discussing XML basics, I think that the trickiest parts for the newbie are namespaces with namespace prefixes, CDATA sections, and validation.

Hope this helps.

Jeff
Hi Paul,

I wrote a small Java application to see what's happening with your code and obtained the following code:



I also created a file containing your XML. The only difference is that I replaced with as the attribute's value. After compiling and running the application (both under Java SE 8u60), I observed the following output:



Clearly, is not assigned to .

Hope this helps.

Jeff
Hi Paul,

Your XML declaration incorrectly assigns true as the value of the optional standalone attribute. The only values that can be legally assigned to this attribute are yes and no, as in "yes" or "no".

Jeff
Hi Ludoviko,

The first six chapters focus on XML and the last six chapters focus on JSON -- there is a clean separation between the two.  I present XML first because it predates JSON and because Java SE provides API support for XML.

I have some thoughts on what to include in a third edition of the book (if there is a third edition). For example, I'd include a chapter that combines XML and JSON because they complement each other. JSON is great for data interchange, whereas XML is great for data modelling.

Jeff
Hi Carl,

I agree with the author of "JSON vs. XML: The battle for format supremacy may be wasted energy" (https://sdtimes.com/communication-protocols/json-vs-xml-battle-format-supremacy-may-wasted-energy/) that JSON is preferred for data interchange, whereas XML is preferred for data modelling, and that they nicely complement each other. You might want to read this article if you haven't already.

Jeff