用 Java 读取 XML 文件内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16053621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading XML file content in Java
提问by Krishna Bhaskar
Can you tell me best way to read an XML file in Java with sample code? XML content be like below.
你能告诉我用示例代码在 Java 中读取 XML 文件的最佳方法吗?XML 内容如下。
<table sourceName="person" targetName="person">
<column sourceName="id" targetName="id"/>
<column sourceName="name" targetName="name"/>``
</table>
回答by Evgeniy Dorofeev
I would use JAXB, try this, it works
我会使用 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);
}
}
回答by Evgeniy Dorofeev
You could use Simpleform simple XML serialization:
您可以使用简单形式的简单 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
:
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
:
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;
}
}
回答by santamanno
Since it's a very small XML file, I would use DOM parsing, you can find a full example here:
由于它是一个非常小的 XML 文件,我将使用 DOM 解析,您可以在此处找到完整的示例:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
But in essence:
但本质上:
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"));
}
}
Please see the relevant imports at the top of the example.
请参阅示例顶部的相关导入。
Hope it helps, A.
希望有帮助,A。
回答by Michael Kay
Books have been written on the subject, and it all depends. JAXB is a good choice if the structure of the file is simple and stable (it maps elements/attributes to Java classes, and you don't want to be changing and recompiling your Java classes three times a week).
关于这个主题的书已经写好了,这一切都取决于。如果文件的结构简单且稳定(它将元素/属性映射到 Java 类,并且您不想每周更改和重新编译 Java 类 3 次),那么 JAXB 是一个不错的选择。
Otherwise there's a range of generic tree models - DOM is the most widely used, oldest, and worst; I would recommend JDOM2 or XOM.
除此之外,还有一系列通用树模型——DOM 是使用最广泛、最古老和最差的;我会推荐 JDOM2 或 XOM。
But the ideal is to avoid reading the data into Java at all; the "XRX" or "end-to-end XML" principle is to use XML-oriented languages such as XSLT and XQuery for the entire application, perhaps calling into Java support routines occasionally if you really need to.
但理想情况是完全避免将数据读入 Java;“XRX”或“端到端 XML”原则是对整个应用程序使用面向 XML 的语言,例如 XSLT 和 XQuery,如果您确实需要,可能偶尔会调用 Java 支持例程。