Meaningless Drivel is fun!*
The moose likes XML and Related Technologies and the fly likes How to parse  xml inpustream? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to parse  xml inpustream?" Watch "How to parse  xml inpustream?" New topic
Author

How to parse xml inpustream?

kabera james
Greenhorn

Joined: May 18, 2011
Posts: 14
I am new in java network and I want to parse xml inputstream but when I' m running this codes to find the Nodechild nothing in output.

Here are my codes. any help I will appreciate, Thank U. I notice that , I don't want to use some classes that remove HTTP header in process.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Why are you building your own socket when java provides the HttpURLConnection?

I would certainly use HttpURLConnection or the HttpClient toolkit to gt the input stream.

My next step would be to capture the full text of the response stream to a local file so I could verify that it was parseable XML.

As it is, you are depending on multiple steps all working correctly with no checking.


Bill

kabera james
Greenhorn

Joined: May 18, 2011
Posts: 14
Hello william. I know that httpurlconnection can do this but the project I have to submit this Friday. The professor demand not use urlconnection because with it everything is done like remove http header. If you see any way I can deal with this thank you for your help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You get no output at all? Sorry, I don't believe that. Either you should have an exception thrown (the most likely outcome based on what I see there) or you should get something starting with "Root Node Name:" in your output. At any rate if you don't understand what that code is doing then you should put in more debugging statements like that.
kabera james
Greenhorn

Joined: May 18, 2011
Posts: 14
Hello Paul. the output I get is only the the root node name. but if you look well . I also make this

NodeList children = document.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
{
String child = children.item(i).getNodeName();

System.out.println(child);

To print the childnode list.

Thank you
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

kabera james wrote:Hello Paul. the output I get is only the the root node name. but if you look well . I also make this...


I see. But you originally said you got no output. That isn't the same thing as getting only the root node name. So did you change the code?
kabera james
Greenhorn

Joined: May 18, 2011
Posts: 14
No I didn't. It seems that I only read the begining and the rest fo the file is empty. which has non sens . many suggestions I got , I dealt with but no change.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't understand what that code is supposed to do anyway. At best it should reprint the root node's name.
kabera james
Greenhorn

Joined: May 18, 2011
Posts: 14
Here is the xml inputstream that I received from this URL "http://twitter.com/statuses/user_timeline/13507732.atom".

The codes suppose to print out the Root no names and his child node name like <title></title>. And then after I will change or adapt the codes to find the content of each nodes.



Thank you

<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns:georss="http://www.georss.org/georss" xmlns="http://www.w3.org/2005/Atom" xmlns:twitter="http://api.twitter.com">
<title>Twitter / sylvainulg</title>
<id>tag:twitter.com,2007:Status</id>
<link type="text/html" href="http://twitter.com/sylvainulg" rel="alternate"/>
<link type="application/atom+xml" href="http://127.0.0.1:8000/statuses/user_timeline/13507732.atom" rel="self"/>
<updated>2011-05-19T16:22:35+00:00</updated>
<subtitle>Twitter updates from sylvainulg / sylvainulg.</subtitle>
<entry>
<title>sylvainulg: http://downrightnow.com/ pour savoir "si c'est normal que (twitter|blogger|...) ne fonctionne pas" sans y passer des heures.</title>
<content type="html">sylvainulg: http://downrightnow.com/ pour savoir "si c'est normal que (twitter|blogger|...) ne fonctionne pas" sans y passer des heures.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/68634436505309184</id>
<published>2011-05-12T11:11:39+00:00</published>
<updated>2011-05-12T11:11:39+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/68634436505309184" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://www.run.montefiore.ulg.ac.be/~martin/Grabbers/feedback.html &lt;-- cotes, erreurs courantes, et instructions pour reproduire les tests.</title>
<content type="html">sylvainulg: http://www.run.montefiore.ulg.ac.be/~martin/Grabbers/feedback.html &lt;-- cotes, erreurs courantes, et instructions pour reproduire les tests.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/58626221206024192</id>
<published>2011-04-14T20:22:35+00:00</published>
<updated>2011-04-14T20:22:35+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/58626221206024192" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: 200 OK : check. 301 Moved : check. 404 : check ... soyons fous: testons le Chunked Encoding ...</title>
<content type="html">sylvainulg: 200 OK : check. 301 Moved : check. 404 : check ... soyons fous: testons le Chunked Encoding ...</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/58472421027618816</id>
<published>2011-04-14T10:11:26+00:00</published>
<updated>2011-04-14T10:11:26+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/58472421027618816" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://www.scinfo.be/index.php?p=topic&t_id=5581 &lt;-- je vais vous demander de faire sans InputSource pour XLRss. Désolé.</title>
<content type="html">sylvainulg: http://www.scinfo.be/index.php?p=topic&t_id=5581 &lt;-- je vais vous demander de faire sans InputSource pour XLRss. Désolé.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/58183213780180993</id>
<published>2011-04-13T15:02:13+00:00</published>
<updated>2011-04-13T15:02:13+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/58183213780180993" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://139.165.223.2/~martin/resources/recvHttpHeaders.java
chose promise, chose due.</title>
<content type="html">sylvainulg: http://139.165.223.2/~martin/resources/recvHttpHeaders.java
chose promise, chose due.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/56379677526327297</id>
<published>2011-04-08T15:35:37+00:00</published>
<updated>2011-04-08T15:35:37+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/56379677526327297" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://www.run.montefiore.ulg.ac.be/~martin/grabbers.md5 &lt;- 54 projets reçus. 80% compilent sans heurts. Encourageant.</title>
<content type="html">sylvainulg: http://www.run.montefiore.ulg.ac.be/~martin/grabbers.md5 &lt;- 54 projets reçus. 80% compilent sans heurts. Encourageant.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/53433473976909824</id>
<published>2011-03-31T12:28:27+00:00</published>
<updated>2011-03-31T12:28:27+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/53433473976909824" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: com.sun.org.apache.xpath.internal.XPathAPI est (comme son nom l'indique) une classe interne. on utilise org.apache.xpath.*. merci.</title>
<content type="html">sylvainulg: com.sun.org.apache.xpath.internal.XPathAPI est (comme son nom l'indique) une classe interne. on utilise org.apache.xpath.*. merci.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/53430775038746624</id>
<published>2011-03-31T12:17:44+00:00</published>
<updated>2011-03-31T12:17:44+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/53430775038746624" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: énoncé du TP XLRss : http://www.run.montefiore.ulg.ac.be/cours/iri/tp-xlrss.pdf</title>;
<content type="html">sylvainulg: énoncé du TP XLRss : http://www.run.montefiore.ulg.ac.be/cours/iri/tp-xlrss.pdf</content>;
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/52355954435555328</id>
<published>2011-03-28T13:06:46+00:00</published>
<updated>2011-03-28T13:06:46+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/52355954435555328" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://www.wireshark.org/docs/wsug_html_chunked/ChBuildInstallWinInstall.html &lt;-- pour la mise au point des redirections, et tout.</title>
<content type="html">sylvainulg: http://www.wireshark.org/docs/wsug_html_chunked/ChBuildInstallWinInstall.html &lt;-- pour la mise au point des redirections, et tout.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/50950200612749312</id>
<published>2011-03-24T16:00:49+00:00</published>
<updated>2011-03-24T16:00:49+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/50950200612749312" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: quelques précisions sur les classes autorisées/interdites/suggérées/obligatoires: http://www.scinfo.be/index.php?p=topic&t_id=5581</title>;
<content type="html">sylvainulg: quelques précisions sur les classes autorisées/interdites/suggérées/obligatoires: http://www.scinfo.be/index.php?p=topic&t_id=5581</content>;
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/47982898460098560</id>
<published>2011-03-16T11:29:49+00:00</published>
<updated>2011-03-16T11:29:49+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/47982898460098560" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: Ajout du lien vers la Javadoc de l'API XPath. En ligne de commande, "java -cp path/to/xalan.jar:. Grabber" pour s'en servir.</title>
<content type="html">sylvainulg: Ajout du lien vers la Javadoc de l'API XPath. En ligne de commande, "java -cp path/to/xalan.jar:. Grabber" pour s'en servir.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/47948039771537408</id>
<published>2011-03-16T09:11:18+00:00</published>
<updated>2011-03-16T09:11:18+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/47948039771537408" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: n'oubliez pas d'enregistrer votre groupe avant le 28 mars: http://www.run.montefiore.ulg.ac.be/cours/iri/groupes/</title>;
<content type="html">sylvainulg: n'oubliez pas d'enregistrer votre groupe avant le 28 mars: http://www.run.montefiore.ulg.ac.be/cours/iri/groupes/</content>;
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/46143081136926720</id>
<published>2011-03-11T09:39:02+00:00</published>
<updated>2011-03-11T09:39:02+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/46143081136926720" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: a fini de corriger la question 1 de SDO. Merci à la CDC pour vendre du papier sur lequel on sait lire les étudiants.</title>
<content type="html">sylvainulg: a fini de corriger la question 1 de SDO. Merci à la CDC pour vendre du papier sur lequel on sait lire les étudiants.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/45430557588463616</id>
<published>2011-03-09T10:27:43+00:00</published>
<updated>2011-03-09T10:27:43+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/45430557588463616" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://www.run.montefiore.ulg.ac.be/cours/iri/tp.html remise à jour. Bon travail sur vos grabbers.</title>
<content type="html">sylvainulg: http://www.run.montefiore.ulg.ac.be/cours/iri/tp.html remise à jour. Bon travail sur vos grabbers.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/43684485031657472</id>
<published>2011-03-04T14:49:27+00:00</published>
<updated>2011-03-04T14:49:27+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/43684485031657472" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://www.run.montefiore.ulg.ac.be/~martin/resources/xlrss.pdf -- premier jeu de transparent prêt pour la répèt de demain.</title>
<content type="html">sylvainulg: http://www.run.montefiore.ulg.ac.be/~martin/resources/xlrss.pdf -- premier jeu de transparent prêt pour la répèt de demain.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/43444048811393026</id>
<published>2011-03-03T22:54:03+00:00</published>
<updated>2011-03-03T22:54:03+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/43444048811393026" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://139.165.223.2/~martin/resources/charTest.java &lt;-- un outil pour en savoir plus sur les Charsets supportés par Java sur votre machine.</title>
<content type="html">sylvainulg: http://139.165.223.2/~martin/resources/charTest.java &lt;-- un outil pour en savoir plus sur les Charsets supportés par Java sur votre machine.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/42603285986820096</id>
<published>2011-03-01T15:13:09+00:00</published>
<updated>2011-03-01T15:13:09+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/42603285986820096" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: Si vous voulez vous avancer pour le TP d'IRI, faites toujours quelques recherches sur le fonctionnement des flux RSS ... ça fera pas de tort</title>
<content type="html">sylvainulg: Si vous voulez vous avancer pour le TP d'IRI, faites toujours quelques recherches sur le fonctionnement des flux RSS ... ça fera pas de tort</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/39977194851667968</id>
<published>2011-02-22T09:18:00+00:00</published>
<updated>2011-02-22T09:18:00+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/39977194851667968" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: http://www.run.montefiore.ulg.ac.be/~martin/SDO/QuickSortB.html &lt;-- une solution à la question 3c de l'examen de mardi.</title>
<content type="html">sylvainulg: http://www.run.montefiore.ulg.ac.be/~martin/SDO/QuickSortB.html &lt;-- une solution à la question 3c de l'examen de mardi.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/28373072998301696</id>
<published>2011-01-21T08:47:22+00:00</published>
<updated>2011-01-21T08:47:22+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/28373072998301696" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: les cotes du 2eme TP sont affichées aux valves, à côté du bureau du Pr. Wolper.</title>
<content type="html">sylvainulg: les cotes du 2eme TP sont affichées aux valves, à côté du bureau du Pr. Wolper.</content>
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/27660722393583616</id>
<published>2011-01-19T09:36:44+00:00</published>
<updated>2011-01-19T09:36:44+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/27660722393583616" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
<entry>
<title>sylvainulg: précisions sur le getchar (qui devient FAQ) : http://bit.ly/eeubMt</title>;
<content type="html">sylvainulg: précisions sur le getchar (qui devient FAQ) : http://bit.ly/eeubMt</content>;
<id>tag:twitter.com,2007:http://twitter.com/sylvainulg/statuses/11776302784712705</id>
<published>2010-12-06T13:37:44+00:00</published>
<updated>2010-12-06T13:37:44+00:00</updated>
<link type="text/html" href="http://twitter.com/sylvainulg/statuses/11776302784712705" rel="alternate"/>
<link type="image/png" href="http://a2.twimg.com/profile_images/49298662/cool_normal.png" rel="image"/>
<author>
<name>sylvainulg</name>
</author>
<twitter:source>web</twitter:source>
<twitter:place/>
</entry>
</feed>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

kabera james wrote:Here is the xml inputstream that I received from this URL "http://twitter.com/statuses/user_timeline/13507732.atom".

The codes suppose to print out the Root no names and his child node name like <title></title>.


But your code prints the name of the first child of the document (which is the root node) and then it proceeds to print information about the children of the document (namely the root node).

If you want to print information about the children of the root node, then change your code to do that.
kabera james
Greenhorn

Joined: May 18, 2011
Posts: 14
Thank you Paul. really I made mistakes I should add this getDocumentElement() to get all child nodes. now I still have another question how to get the text means the contents of each nodes. here are the ouputs.


Root Node Name: feed
#text
title
#text
id
#text
link
#text
link
#text
updated
#text
subtitle
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
entry
#text
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That means you have a text node (which is just the whitespace between two elements) and then a "title" element and then a text node (more whitespace) and then an "id" element, and so on.

The "title" element has a single child, which is a text node containing "Twitter / sylvainulg". If you want to get that text... well, you already know how to get a child node of something. If you want to get the text which is contained in a Text node, then check out the API documentation for a method which does that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to parse xml inpustream?
 
Similar Threads
Mimic Browser Httprequests & posts
Simple program for URL
GET html contents from a web server
Problem in file transfer
How to parse xml inpustream