Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Java XML & JSON - FileInputstream is null  RSS feed

 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
As you are promoting a book on XML,I might as well ask you about  a current problem I'm dealing with.

I'm passing in a file to the code below and  after the try block ,the variable "doc" is null but not throwing an error.
The document I'm passing in is valid xml (according to an online xml validator and our business partner that validates and uses the file).
However, after the call to parse() , the "doc" variable is null .

Any idea of what is going on?
I've copied pasted the xml file below with some changed values.

The idea is I'm going to use the doc variable as such:


thanks,
Paul


 
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

paul nisset wrote:However, after the call to parse() , the "doc" variable is null .



That would happen if an exception were thrown. And your post title strongly suggests that's exactly what happened.
 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that a File object doesn't have to correspond to an actual file in your computer's file system.
 
Author
Posts: 86
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeffrey Friesen
Author
Posts: 86
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Jeff ,
Thanks for looking at it .
I don't understand your point . It's not clear how doc is not assigned the value of null .Your output indicates that doc is null after running your modified version. That is the problem .

In terms of 'standalone' being assigned true that was generated by the function that creates the file .Specifically


and running the application (both under Java SE 8u60), I observed the following output:


[#document: null]


Clearly,

null
is not assigned to
doc





thanks,
Paul
 
Jeffrey Friesen
Author
Posts: 86
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Jeffrey Friesen]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[/quote]

Hi,
I'm just looking at it now in Eclipse and also getting [#document: null]" after the assignment in the try block. If I understand correctly, you are saying this means the object is not null and the
[#document: null]" is the result of it's toString method ?
I would have expected toString() to return a memory address of the doc object.

The line following the try block is
[code=java]Element element = (Element) doc.getElementsByTagName("pm1a").item(0);[/code]
and element is null after the assignment but it has a value in the xml document being parsed.

I don't see how the doc variable is not null at this point.
-Paul


 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Paul Clapham wrote:

paul nisset wrote:However, after the call to parse() , the "doc" variable is null .



That would happen if an exception were thrown. And your post title strongly suggests that's exactly what happened.



Hi Paul,
The assignment to 'doc' doesn't cause the program step into the exception block despite 'doc' being null after the assignment. I would have expected it to throw  an exception as well.

The assignment to fis produces an object without throwing an exception .

I guess my question is for the task of parsing an xml document and assigning the elements to Element variables, am I doing this correctly?

If not, how should I be doing this ?

Thanks,
Paul
 
Jeffrey Friesen
Author
Posts: 86
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff.
It seems like a strange toString() output for a Document object.
In light of that ,do you know why the following line in Eclipse hovering over element after the assignment ,I get " [adminappv:null] " when the text in the document's element is "0"?
As you suggested with the doc object ,element is not null after the assignment.


When I look at element.getNodeValue() it returns null .
How would I get the value of 'adminappv' I see in the document ?

adminappv in the document is "0" .

The over all task I am doing is trying to update elements or omit elements in the xml document based on their values and then update the document before submitting it to our business partner.

Maybe I'm approaching the problem incorrectly .

thanks,
Paul



 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

paul nisset wrote:In light of that ,do you know why the following line in Eclipse hovering over element after the assignment ,I get " [adminappv:null] " when the text in the document's element is "0"?



It's because you are confusing the document element node with the text node which happens to be its only child. Sure, the value of that text node is "0", but the value of the element node which is its parent is not that. Consider this XML document:



What should the value of the "base" element node be? Well, there's a lot of things you might come up with but the fact is that the value of the "base" element node is null. And in fact the value of every element node is null. Frankly the whole concept of the "value" of a node isn't very useful.
 
Jeffrey Friesen
Author
Posts: 86
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 725
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@paul nisset
[1]
I return to the exchanges at the stage where standalone is wrongly written as true instead of, as rightly corrected by the invited author, as yes.

paul nisset 2019-02-13 4:05:22 AM wrote:Hi Paul,
The assignment to 'doc' doesn't cause the program step into the exception block despite 'doc' being null after the assignment. I would have expected it to throw  an exception as well.

The assignment to fis produces an object without throwing an exception .

I guess my question is for the task of parsing an xml document and assigning the elements to Element variables, am I doing this correctly?  


You certainly would have discovered an exception was being raised. But look at your code (and compare it with what shown by the invited author):

paul nisset wrote:


There is a closing curly bracket before catch... if the code even work! But if code properly, you would have seen the exception thrown with standalone="true", saying:

And you just have to thank the invited author and the matter would be closed... all the getElementsByTagName() etc... would work out correctly. The jaxb stuff is just a diversion. I return to this just to disperse wrong impressions the exchanges made to casual readers.

[2]

paul nisset wrote:When I look at element.getNodeValue() it returns null .
How would I get the value of 'adminappv' I see in the document ?  


You can do it like this.
 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

paul nisset wrote:In light of that ,do you know why the following line in Eclipse hovering over element after the assignment ,I get " [adminappv:null] " when the text in the document's element is "0"?



It's because you are confusing the document element node with the text node which happens to be its only child. Sure, the value of that text node is "0", but the value of the element node which is its parent is not that. Consider this XML document:



What should the value of the "base" element node be? Well, there's a lot of things you might come up with but the fact is that the value of the "base" element node is null. And in fact the value of every element node is null. Frankly the whole concept of the "value" of a node isn't very useful.



That explains  why I'm getting null. Not sure I understand it though. Especially as the Element object has both a setNodeValue() and getNodeValue() .
In your example,to set the value of  the node "and" would I do do something like   (Element) doc.getElementsByTagName("base").getFirstChild().setValue("somethingotherthanDEF") ?

thanks,
Paul
 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeffrey Friesen wrote: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):



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.





Thank you Jeff. I think the aspect I was missing is the getElementFirstChild . It looks like  PaulC. was making the same point .ie I want to access  a child node of  "adminappv" . In this case, a text node. Good luck with the book.

-Paul
 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

g tsuji wrote:@paul nisset
[1]
I return to the exchanges at the stage where standalone is wrongly written as true instead of, as rightly corrected by the invited author, as yes.

paul nisset 2019-02-13 4:05:22 AM wrote:Hi Paul,
The assignment to 'doc' doesn't cause the program step into the exception block despite 'doc' being null after the assignment. I would have expected it to throw  an exception as well.

The assignment to fis produces an object without throwing an exception .

I guess my question is for the task of parsing an xml document and assigning the elements to Element variables, am I doing this correctly?  


You certainly would have discovered an exception was being raised. But look at your code (and compare it with what shown by the invited author):

paul nisset wrote:


There is a closing curly bracket before catch... if the code even work! But if code properly, you would have seen the exception thrown with standalone="true", saying:

And you just have to thank the invited author and the matter would be closed... all the getElementsByTagName() etc... would work out correctly. The jaxb stuff is just a diversion. I return to this just to disperse wrong impressions the exchanges made to casual readers.

[2]

paul nisset wrote:When I look at element.getNodeValue() it returns null .
How would I get the value of 'adminappv' I see in the document ?  


You can do it like this.




Regarding point [1]. I was using a library. That's what the library generated. It doesn't always add "standalone="true" . Other documents it generated don't have that . The code I presented, ran without the exception block being called.

Regarding point [2] - It seems the take away point from Jeff and Paul C. is that I was looking for a value from a parent Node and not a child Node. ie "adminappv" is the  parent node of the text node that contains the value I was looking for.

thanks,
Paul
 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

paul nisset wrote:Regarding point [2] - It seems the take away point from Jeff and Paul C. is that I was looking for a value from a parent Node and not a child Node. ie "adminappv" is the  parent node of the text node that contains the value I was looking for.



Yes, that's right. A pretty common stumbling block when you're starting with XML. Let me warn you in advance about another XML gotcha:



In this XML fragment  there are three things:

1. An <adminappv> element which contains a text node.

2. A text node which contains only white space.

3. An <agencyid> element which contains a text node.

Many people overlook those white-space text nodes and write code which asks for "the seventh child of the root element". That leads to confusion because they didn't include the white-space text nodes in their count.
 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's interesting.
It would not have occurred to me that line 2 is a text node.
Thank you.
 
g tsuji
Ranch Hand
Posts: 725
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The print out of doc being null in case where xml is not well-formed and that being [#document: null] if it is guaranteed well-formedness and parsed are obviously two different things.

In case you are wondering what is it, the [#document: null], and how to properly interprete it ? This is how: where the library implement org.w3c.dom.Node interface, you can have its default toString() implemented as you decide to show. Here it is implementation dependent. What you see reflects the implementation:

where #document is the "node name" of the abstract Document, and Document's node value is null by design and by w3c recommendation compliance. For an element, say canProject it will show something like this:

it is null there because element node is by design and by recommendation compliance has node value null.

Schematically, its output are obtained through the method figuratively shown below:


If you're at a text node, the second component will show something with node name showing #text for text node by design and by compliance... again. You can discover the rest for comment, processinginstruction and cdata nodes. There is nothing secretive about it.

That's all about it.
 
paul nisset
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks g tsuji.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!