Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: How to read and parse a remote XML file with Java

Status
Not open for further replies.

guarriman

Programmer
Aug 31, 2007
2
ES
Hi.

Using J2SE v1.4.2, I'd like to read and parse a remote file:

Is it possible with Java? I'd be extremely grateful if someone could provide me any webpage showing a very simple code.

Thank you very much.
 
>>Using J2SE v1.4.2, I'd like to read and parse a remote file:
>>>No, it is not possible. You can only read local files.

I'm not sure I would agree if it is not only a matter of wording of local file in a sense being something not very dissimilar to stream loaded to the local memory. Most parsers that I've come aware of support one way or another remote at least through http protocol (and network urn...)

Take an example of parsing local file that come handy at this moment:

I would therefrom add some standard classes of j2se 1.4.2 so that it becomes a smal application that can read and parse the same xml file "remote" on say. Those changes I indicated in blue.

[1] book.xml
[tt]
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>
[/tt]
[2] java code
[tt]
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

[blue]//tsuji:note [a] adding these
import java.net.URL;
import java.io.InputStream;[/blue]

public class ReadAndPrintXMLFile{
public static void main (String argv []){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

[blue]//tsuji:note change this
//Document doc = docBuilder.parse (new File("book.xml"));[/blue]
[blue]URL url = new URL(" InputStream stream = url.openStream();
Document doc = docBuilder.parse(stream);[/blue]

// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());
//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : " +
((Node)textLNList.item(0)).getNodeValue().trim());
//----
NodeList ageList = firstPersonElement.getElementsByTagName("age");
Element ageElement = (Element)ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : " +
((Node)textAgeList.item(0)).getNodeValue().trim());
//------
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
[/tt]
 
Hi

I think you are right about being just matter of wording. While the HTTP protocol does not support remote file opening, the applications must download it, even if behind some abstraction levels. But certainly your reply is closer to guarriman's needs.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top