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 OverflowThere 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.
How to read this xml file in java (need to be done by using exported,firstName,lastName,...)? - Stack Overflow
Reading XML file content in Java - Stack Overflow
Is there an easy way to read an XML file in Java? - Stack Overflow
How to read XML file using java - Stack Overflow
Videos
use like below class --
this.getClass().getClassLoader().getResourceAsStream("path of file");
NOTE : this xml file structure in project
+pro
+src
+resource
-test.xml
Example:
public class ReadFile {
public void testReadFile() {
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("/Resources/tset.xml");
//praparing DOM
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
// by sending input stream as input to DOM
Document doc = docBuilder.parse(in);
}
public static void main(String[] args) {
ReadFile file = new ReadFile();
file.testReadFile();
}
}
You can use the structure of maven : Project/src/main/resources
I suppose it is a data xml file. If it is a config file a better way is to access it via the classpath. This is a common place where to put the resources, but you can choose your own directory. If you have problem to open the file, it is your path which is not correct. Try first to open it with a absolute path. Something like d:/workspace/Project/src/main/resources/file.xml. It will works.
I would use JAXB, try this, it works
public class Test1 {
@XmlAttribute
String sourceName;
@XmlAttribute
String targetName;
@XmlElement(name = "column")
List<Test1> columns;
public static Test1 unmarshal(File file) {
return JAXB.unmarshal(file, Test1.class);
}
}
You could use Simple form simple XML serialization:
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class App {
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<table sourceName=\"person\" targetName=\"person\">\n"
+ " <column sourceName=\"id\" targetName=\"id\"/>\n"
+ " <column sourceName=\"name\" targetName=\"name\"/>``\n"
+ "</table>";
Serializer serializer = new Persister();
Table table = serializer.read(Table.class, xml);
System.out.println(table.getSourceName());
System.out.println(table.getTargetName());
for (Column colunmn : table.getColumns()) {
System.out.println(colunmn.getSourceName());
System.out.println(colunmn.getTargetName());
}
}
}
Table:
import java.util.List;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root(name = "table")
public class Table {
@Attribute
private String sourceName;
@Attribute
private String targetName;
@ElementList(name = "column", inline = true)
private List<Column> columns;
public Table() {
}
public String getSourceName() {
return sourceName;
}
public void setSourceName(String sourceName) {
this.sourceName = sourceName;
}
public String getTargetName() {
return targetName;
}
public void setTargetName(String targetName) {
this.targetName = targetName;
}
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
Column:
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
@Root(name = "column")
public class Column {
@Attribute
private String sourceName;
@Attribute
private String targetName;
public Column() {
}
public String getSourceName() {
return sourceName;
}
public void setSourceName(String sourceName) {
this.sourceName = sourceName;
}
public String getTargetName() {
return targetName;
}
public void setTargetName(String targetName) {
this.targetName = targetName;
}
}
I've written a very simple API for precisely this reason. It uses the DOM parser underneath, but exposes a very simple and easy-to-use API that allows you to get to the XML data really easily. It's just a single Java file that you can use as a library in your code. Hope that helps.
http://argonrain.wordpress.com/2009/10/27/000/
I would recommend the Commons Configuration library: http://commons.apache.org/configuration/index.html Take a look at the HOWTOs to see how easy it is to get some information from an XML file.
All other libs I know involve either operating on the DOM directly or registering handlers for SAX parsing (which both involve a high overhead of code). JAXB is also an alternative but doesn't involve less overhead code than the former two.
Max