Posted: Wed Sep 23, 2009 4:35 pm Post subject: Java and XML
I'm wondering if in a RTP java program I can retrieve information from an XML website and utilize it in a program. I know if you're using JavaScript it's more approachable (I'd assume), but I want to do it in an executable. I'm fairly new to Java, and I'm looking to know if this is relatively possible. Thanks.
Sponsor Sponsor
syntax_error
Posted: Wed Sep 23, 2009 4:50 pm Post subject: RE:Java and XML
OR if you want a hard copy of it you can pay 54 something at your nearest book store.
DemonWasp
Posted: Wed Sep 23, 2009 8:03 pm Post subject: RE:Java and XML
It's pretty trivial, and actually easier than in JavaScript (as far as I know). Just look up DocumentBuilder and DocumentBuilderFactory, which you use to produce a Document, which you use to get a root Element.
Example:
Java:
File myXMLFile = new File ( "config/config.xml" );
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse ( myXMLFile );
Element root = doc.getDocumentElement();
After that, root is the outermost tag in the XML file.
Alexander
Posted: Mon Sep 28, 2009 2:35 pm Post subject: RE:Java and XML
Thanks a lot to both of you, both replies helped, I'll be sure to check out that e-book. But I'm not sure you understood what I wanted. I'm looking to go to a website(s) that isn't administered by me, and retrieve simple public information in strings.
Basically an executable that would access the internet, and get information from a page's xml (and after presenting it simply).
Possible?
DemonWasp
Posted: Mon Sep 28, 2009 2:58 pm Post subject: RE:Java and XML
If you're looking to read information from a website, that's really easy too. Assuming that it's well-formed XML, then my previous post should tell you how to parse it.
The following should let you pull an XML file from a known URL:
StringBuilder result = new StringBuilder();
Scanner scanner = new Scanner( in );
for ( String line = scanner.nextLine(); scanner.hasNextLine(); line = scanner.nextLine() ) {
if ( line.trim().length() > 0 ) {
result.append( line ).append ( "\n" );
}
}
Alexander
Posted: Mon Sep 28, 2009 8:13 pm Post subject: RE:Java and XML
I'm having trouble figuring out what was needed to be imported for that section of code to be used.. I'm assuming import java.io.DataInputStream, but StringBuilder, URL page, and Scanner are all giving me problems and I don't know how to declare them. I searched around and tried import java.util.Scanner, it said it wasn't valid. And the Scanner Java documentation doesn't explicitly say how to import for it..shouldn't it? I skimmed it and searched "import" with not avail.
DemonWasp
Posted: Mon Sep 28, 2009 8:43 pm Post subject: RE:Java and XML
Look at the Scanner Javadoc. See where it says "java.util" and then the class name is Scanner? That means that import java.util.Scanner is correct, and the problem lies elsewhere.
Notice a little lower, where it says "Since: 1.5"? That means that this has only been in Java since version 1.5. If you're running 1.4 or less (which is the case if you're using Ready to Program) then the class doesn't exist in your JVM. I would recommend upgrading to a new one if possible (and a new IDE if you're using Ready...or no IDE at all).
If Scanner isn't available, you'll have to make your own solution with BufferedReader or similar classes.
You can find the other imports easily enough at the Java API Documentation Page. I have linked version 1.6, but you should be able to find other versions pretty easily. For example, URL is java.net.URL. Read the documentation! It will help.
OneOffDriveByPoster
Posted: Mon Sep 28, 2009 8:56 pm Post subject: Re: Java and XML
This is a event driven model (like Swing), so you just have to implement handlers for things you are interested in.
Sponsor Sponsor
Alexander
Posted: Mon Sep 28, 2009 9:48 pm Post subject: RE:Java and XML
I knew it was something like that.. I attempted to save after and RTP crashed. Figures... The IDE is junk. Now I'm on a quest to find a nice portable Java IDE.
DemonWasp
Posted: Mon Sep 28, 2009 10:06 pm Post subject: RE:Java and XML
You probably want Eclipse, NetBeans, JCreator or somesuch. I can't speak for the latter two, but Eclipse is as cross-platform as they come...Windows, OSX, AIX, Solaris, Linux. I don't know about BSD, but I wouldn't be surprised.
Be warned: Eclipse and NetBeans are the tools that (some) professionals use. They have a lot of things to them that have some very complicated and powerful capabilities. Don't worry about learning the whole environment - just learn what you're using.
andrew.
Posted: Tue Sep 29, 2009 3:00 pm Post subject: RE:Java and XML
Since you're a beginner and you're using RTP, I would suggest JCreator or JGrasp. I personally like JGrasp better because it feels faster and cleaner and it's also cross platform.
Alexander
Posted: Tue Sep 29, 2009 3:26 pm Post subject: Re: RE:Java and XML
DemonWasp @ Mon Sep 28, 2009 10:06 pm wrote:
You probably want Eclipse, NetBeans, JCreator or somesuch. I can't speak for the latter two, but Eclipse is as cross-platform as they come...Windows, OSX, AIX, Solaris, Linux. I don't know about BSD, but I wouldn't be surprised.
Be warned: Eclipse and NetBeans are the tools that (some) professionals use. They have a lot of things to them that have some very complicated and powerful capabilities. Don't worry about learning the whole environment - just learn what you're using.
I'm looking at NetBeans. I read Eclipse is a monster to load.. and I have to wait for Microsoft Word to open on my schools computers. NetBeans has Java, JavaSE, JavaFX versions. I dont want to get the plain Java one since it's the largest file and will probably be a pain to load, are the others utilizable to my needs?
andrew. @ Tue Sep 29, 2009 3:00 pm wrote:
Since you're a beginner and you're using RTP, I would suggest JCreator or JGrasp. I personally like JGrasp better because it feels faster and cleaner and it's also cross platform.
Are they portable programs, and support a higher version of Java than RTP ?
andrew.
Posted: Tue Sep 29, 2009 3:36 pm Post subject: RE:Java and XML
1. What do you mean by portable? If you mean that they run off USB drives without installation then no, you have to install them. If you want something portable, just use Notepad and Command Line.
2. They support whatever version of Java you have on your computer. You must install the JDK yourself as they don't come with it built-in like RTP.
DemonWasp
Posted: Tue Sep 29, 2009 4:03 pm Post subject: RE:Java and XML
Eclipse loads in about 10 seconds on a midrange machine (P4, 1GB ram...), or 3-4 on a real machine. It may take additional time to load up your workspace, but usually only a second or so. It's pretty fast (it loads somewhat faster than the only other IDEs I've ever used, VB6 and Borland C++ Builder 6).
Eclipse will also run just fine off a USB drive (or pretty well any other folder) without being installed. You unzip it and run the "eclipse" executable, and it runs. Done!
Andrew is correct, however...these programs will rely on external JDKs which you will have to either install or already-have-installed yourself. Type in java -version or, more importantly, javac -version at the command line to find out what versions you have.
Alexander
Posted: Tue Sep 29, 2009 4:31 pm Post subject: RE:Java and XML
I'm well aware of both references to the commandline--thing is: command prompt is disabled on these machines along with a lot of things.
But I didn't know it relied on external JDK. That's no fun, even though it's free, I'm sure they wont get it installed.
Thanks for your help guys. I guess I'll approach my more in-depth projects on my own computer. (After a jab at Eclipse)
EDIT: She's getting a little off topic here, but, what JDK am I looking for. I see all this SE and FX jazz. http://openjdk.java.net/ Is this what I need?