What's the easiest way to simply read in values from simple XML?
EDIT: I realize there is quite a bit of information and other topics on XML parsing out there, however I was hoping somebody who knows what they are talking about might be able to give me the deffinative answer as to the SIMPLIST way to do this (with least ammount of code and required imports, etc)
I have a simple application that I want to have read "settings" in from a simple XML file.
the XML file looks something like this:
<dbconnection>XXX.XX.XXX.XX</dbconnection>
<dbusername>lololololol</dbusername>
<dbpass>kekekekekekeke</dbpass>
What is the MOST simple possible way to read in values like this from an external XML file?
Thanks in advance! <3
Message was edited by:
shoelessone
http://java.sun.com/xml/tutorial_intro.html
That looks like XML config to me. It depends what "simplest" means, really. If you don't consider downloading a third-party jar complicated, your best bet for this is probably Apache Digester. All you need is a class that has properties matching your XML, and Digester will populate an instance of it from the XML. Designed exactly for using XML to configure stuff, and you won't need to bother actually reading XML in and matching element names to methods etc
Wow, lots of good information there!
I'll look it over. If anybody has any example code they use though, that would be awesome. As much as I hate to sound like a whiney *****, I just need xml for a very small little thing and it'd be nice to not have to learn all about xml parsing to input those three bits of data :)
Thanks again!
This is pretty simpleFile f = ...DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse(f);
Apache Digester?Is that a non-standard java library? I'm assuming yes obviously...I'm hoping to only use standard java libraries with this small application...
Once the "document factory" parses the file though, how do I get the values out of the "document factory"?
I should point out that I'm basically brand new to Java :(
Also these forums, so I'm sorry if I'm out of line. Perhaps I should actually be posting in the "new to java programming" forum?
> Apache Digester?
>
> Is that a non-standard java library? I'm assuming
> yes obviously...
>
> I'm hoping to only use standard java libraries with
> this small application...
Yeh, it is. Wanting to avoid a third-party dependency is reasonable enough, but I wouldn't make it a hard-and-fast rule. You should weigh up the cost of downloading and using a tiny jar against whittling it yourself, there isn't really a best practice. But don't you need a third-party library to parse XML anyway, or does the JDK now ship with a parser? I honestly don't know. Using Apache Digester will involve using a third-party jar, but it will allow you to avoid writing any XML-parsing code.
Doesn't really matter, you've got several options from here, any one of which will do the trick. But I'd be wary of avoiding third-party libraries just for the sake of avoiding them, they can make your life a h3ll of a lot simpler!
The first reply was to a tutorial. Don't expect forum members to write your code for you. Have you mastered that tutorial already?
> Once the "document factory" parses the file though,
> how do I get the values out of the "document
> factory"?
>
> I should point out that I'm basically brand new to
> Java :(
>
> Also these forums, so I'm sorry if I'm out of line.
> Perhaps I should actually be posting in the "new to
> java programming" forum?
Nope, you're doing just fine
You get an instance of Document, which is the representation of a DOM tree. So you can ask for the root element by calling document.getDocumentElement(), for instance, and walk through the whole tree. http://en.wikipedia.org/wiki/Document_Object_Model
dont learn about xml parsing, use regular expressions.. and here is that example code you were groveling for:
read into a String or StringBuilder with this:
http://doesthatevencompile.com/current-projects/code-sniplets/ASCIIFile.htm
and then parse out the xml elements like this:
public String parseImgLabel(String line){
line = line.replaceAll("\\s+", " ");
Pattern p = Pattern.compile("(?im)\"attrName\">[ ]*<value>[ ]*(.+?)[ ]*</value>");
Matcher m = p.matcher(line);
if(m.find()){
return m.group(1);
}
return null;
}
> dont learn about xml parsing, use regular expressions.. You like to make fun of noobs, dontcha?
Hi there,An alternative that you might be looking for: Dom4J SAX reader + XPath. It's fast and easy: http://www.dom4j.org/apidocs/org/dom4j/io/SAXReader.htmlCheers,yc
> Hi there,
>
> An alternative that you might be looking for: Dom4J
> SAX reader + XPath. It's fast and easy:
>
> http://www.dom4j.org/apidocs/org/dom4j/io/SAXReader.ht
> ml
>
> Cheers,
> yc
Bit overkill for a couple of lines of config, though. Plus, it involves learning Xpath, although that's a handy thing to know about, and this might be as good a time as any to dive in
> > dont learn about xml parsing, use regular
> expressions..
>
> You like to make fun of noobs, dontcha?
sometimes using an xml parser to parse xml is not the right way to go. for example to make a quick hack in under 5 mins that you will forget about in 10. this guy seemed to want something like that
If the xml is that simple you could just replace it with a properties file and be done.
InputStream is = new FileInputStream(new File("path to file"));
Properties props = new Properties();
props.load(is);
Properties is a hash map so you can then simply get the property values using
String value = props.getProperty("the key for the value I want");
(java.util.Properties)
matfud
> If the xml is that simple you could just replace it
> with a properties file and be done.
Thanks for the tip - this seems to work pretty well, but I'm not sure how to make comments in a properties file. Anytime I make line breaks I get errors, and I have NO idea (again) how to comment my propertes file.
Also, back to the XML parsing:
The file I want is basically like this (again :)):
<production>
<ip>XXX.XXX.X.XX</ip>
<domain>DOMAIN</domain>
<user>username</user>
<pass>passwordplaintextlol</user>
</production>
<test>
<ip>XXX.X.XXX.XXX</ip>
<domain>adfklsfasd</domain>
<user>userlolname</user>
<pass>lol.lol.haha</pass>
</test>
So, basically I just want to have something that I say, "
String[] settings = getSettings("test");
and ta da! I have all of my stuff in the aray...
AND, I tried this code (thanks everybody!)
File f = new File("test.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(f);
This seems to ALMOST SORT OF work...
..But I can't seem to get this to work completly. My XML is formatted as in my examples above, and when I do things like (my exact syntax might be a bit off) document.getNumElements() it will return 8 (corrent number) however I have been totally unable to get the actual contents of each tag. So for instance I get do "document.getElementByTag("ip").toString()" and "null" is always returned. No matter what I do, I can get the correct tag name but I ALWAYS get "null". So for instance,
(ip: null)or (test: null), etc.
Any help would be much appreciated :)
propertiy files follow the basic rules:
if a line starts with '#' then it is a comment.
otherwise the line should be of the form
a=b
where 'a' is the name of the property (the equivalent of your xml element name)
and 'b' is tyhe value of the property (equivalent to the contents of your xml element)
matfud
If you want your properties in xml format, java.util.Properties can also load from an xml file:
loadFromXML(InputStream in)
You would need to modify your file to the expected format. For Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Hi</comment>
<entry key="foo">bar</entry>
<entry key="fu">baz</entry>
</properties>
-Tricia
> If you want your properties in xml format,
> java.util.Properties can also load from an xml file:
>
> > loadFromXML(InputStream in)
>
>
> You would need to modify your file to the expected
> format. For Example:
> > <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE properties SYSTEM
> "http://java.sun.com/dtd/properties.dtd">
> <properties>
><comment>Hi</comment>
> <entry key="foo">bar</entry>
><entry key="fu">baz</entry>
> operties>
>
>
> -Tricia
Thanks for pointing that out. I had not reasilsed this functionality had been added :)
JDK 1.5 and above
matfud