There are of course a lot of good solutions based on what you need. If it is just configuration, you should have a look at Jakarta commons-configuration and commons-digester.
You could always use the standard JDK method of getting a document :
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
[...]
File file = new File("some/path");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
Answer from Guillaume on Stack OverflowWhy is Java so addicted to XML?
What are some well maintained XML libraries?
Videos
There are of course a lot of good solutions based on what you need. If it is just configuration, you should have a look at Jakarta commons-configuration and commons-digester.
You could always use the standard JDK method of getting a document :
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
[...]
File file = new File("some/path");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
XML Code:
<?xml version="1.0"?>
<company>
<staff id="1001">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>
Java Code:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("/Users/mkyong/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : "
+ eElement.getAttribute("id"));
System.out.println("First Name : "
+ eElement.getElementsByTagName("firstname")
.item(0).getTextContent());
System.out.println("Last Name : "
+ eElement.getElementsByTagName("lastname")
.item(0).getTextContent());
System.out.println("Nick Name : "
+ eElement.getElementsByTagName("nickname")
.item(0).getTextContent());
System.out.println("Salary : "
+ eElement.getElementsByTagName("salary")
.item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
----------------
Root element :company
----------------------------
Current Element :staff
Staff id : 1001
First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000
Current Element :staff
Staff id : 2001
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000
I recommended you reading this: Normalization in DOM parsing with java - how does it work?
Example source.
My primary job is working on Java applications for a large business. It is what you would call "the enterprise". I deal a lot with such fun things as Java EE, Tomcat/JBoss/WebSphere and Spring on a daily basis. Nearly everything I do on my day to day has me diving into XML. My maven build scripts are thousands of lines of xml (Ant wasn't much better either). My server configuration files are XML. Heck, my application, if youre using Spring, is probably a bunch of XML files with some handwritten Java scattered about. Even worse when you get into the kitchen sink aspects of spring where literally your'e application's execution flow is XML (Spring Integration, Spring Batch, and Camel routes). I feel like I'm not even a Java programmer anymore
My question is two fold:
How and why is Java so addicted to XML?
Is there any communities or open source projects that are presenting alternatives? The Groovy and Scala communities seem to have largely abandoned heavy XML configurations and favored DSL's or as expressing configuration as just code.
PS: On the Maven thing I know their is Gradle as an alternative, but it doesn't have the mindshare where a risk averse enterprise will train employees on "new" tools when their developers have already been using a functioning one for years
So I was poking through the files of a few games, namely FTL, and I noticed they had a lot of their data organized into XML files.
Now I understand that XML files are for organizing data, I just don't understand how this data gets pulled from those files into a program to be used.
What do people use to get the data from the files, and implement it into code, namely in Java, as that's the language I am currently most familiar with.
Edit: Also, is it common to use XML files? Is there something better that's come out?
I'm looking for XML libraries that will be supported but google search turned up a graveyard of old or abandoned libraries. Does anyone have any current recommendations that won't be dropped?