您能告诉我使用示例代码在Java中读取XML文件的最佳方法吗? XML内容如下所示。
<table sourceName="person" targetName="person">
<column sourceName="id" targetName="id"/>
<column sourceName="name" targetName="name"/>``
</table>
答案 0 :(得分:3)
我会使用JAXB,尝试这个,它可以工作
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);
}
}
答案 1 :(得分:1)
您可以使用Simple形式的简单XML序列化:
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;
}
}
答案 2 :(得分:0)
由于它是一个非常小的XML文件,我会使用DOM解析,你可以在这里找到一个完整的例子:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
但实质上:
File fXmlFile = new File("/Users/mkyong/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList nList = doc.getElementsByTagName("table");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node tableNode = nList.item(temp);
Element tableElement = (Element) tableNode;
System.out.println("Table source name: " + tableElement.getAttribute("sourceName"));
System.out.println("Table target name: " + tableElement.getAttribute("targetName"));
NodeList columnList = tableElement.getElementsByTagName("column");
for (int j = 0; j < columnList.getLength(); j++) {
Node columnNode = columnList.item(j);
Element columnElement = (Element) columnNode;
System.out.println("Column source name: " + columnElement.getAttribute("sourceName"));
System.out.println("Column target name: " + columnElement.getAttribute("targetName"));
}
}
请参阅示例顶部的相关导入。
希望它有所帮助, 甲
答案 3 :(得分:0)
关于这个主题的书已经写完了,这一切都取决于。如果文件的结构简单而稳定(它将元素/属性映射到Java类,并且您不希望每周更改和重新编译Java类三次),则JAXB是一个不错的选择。
否则有一系列通用树模型 - DOM是使用最广泛,最古老和最差的;我推荐JDOM2或XOM。
但理想的是避免将数据读入Java; “XRX”或“端到端XML”原则是为整个应用程序使用面向XML的语言,如XSLT和XQuery,如果你真的需要,可能偶尔会调用Java支持例程。