繁体   English   中英

如何在Java中读取xml文件的内容

[英]How to read content of an xml file in Java

我已经创建了一个XML文件和DTD,可以在这里找到。

我已经编写了一个代码,但是它可以工作到一个级别,然后它不能正常工作。 我还创建了一些对象来存储xml文件的值。 但是我只能遍历直到xml的工作sheet标签,然后它才能正常工作。

Recon recon = new Recon();

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(configFile);
doc.getDocumentElement().normalize();

System.out.println("Root Element : " + doc.getDocumentElement().getNodeName());
String outputPath = doc.getDocumentElement().getAttribute("outputPath");
String withCompareFilePath = doc.getDocumentElement().getAttribute("withCompareFile");
String toCompareFilePath = doc.getDocumentElement().getAttribute("toCompareFile");

recon.setOutputPath(outputPath);
recon.setToCompareFile(new File(toCompareFilePath));
recon.setWithCompareFile(new File(withCompareFilePath));

NodeList sheetNodeList = doc.getElementsByTagName("sheet");

List<ReconSheet> reconSheets = new ArrayList<ReconSheet>();

for(int i = 0; i< sheetNodeList.getLength() ; i++) {
  Node tempNode = sheetNodeList.item(i);
  ReconSheet reconSheet = new ReconSheet();
  NamedNodeMap attMap = tempNode.getAttributes();
  Node sheetNode = attMap.getNamedItem("sheetNumber");
  String sheetNumber = sheetNode.getNodeValue();
  reconSheet.setSheetNumber(Integer.parseInt(sheetNumber));
  NodeList list = tempNode.getChildNodes();
  for(int j = 0; j< list.getLength(); j++) {
    Node inNode = list.item(j);
    System.out.println(inNode);
  }
}

注意:我是EclipseLink JAXB(MOXy)的负责人,并且是JAXB 2(JSR-222)专家组的成员。

您可以使用JAXB实现将XML直接映射到域模型。 JAXB需要Java SE 5,并且Java SE 6中包含JAXB实现。

侦察

您的Recon类如下所示:

package forum7673323;

import java.io.File;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Recon {

    @XmlAttribute
    private String outputPath;

    @XmlAttribute
    @XmlJavaTypeAdapter(FileAdapter.class)
    private File withCompareFile;

    @XmlAttribute
    @XmlJavaTypeAdapter(FileAdapter.class)
    private File toCompareFile;

    @XmlElement(name="sheet")
    private List<ReconSheet> reconSheets;

}

侦查表

package forum7673323;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

@XmlAccessorType(XmlAccessType.FIELD)
public class ReconSheet {

    @XmlAttribute
    int sheetNumber;
}

文件适配器

由于JAXB实现不能直接与java.io.File对象进行交互,因此我们将使用JAXB适配器来处理此转换。 使用@XmlJavaTypeAdapter批注在Recon类上指定此适配器的使用:

package forum7673323;

import java.io.File;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class FileAdapter extends XmlAdapter <String, File>{

    @Override
    public String marshal(File file) throws Exception {
        if(null == file) {
            return null;
        }
        return file.getPath();
    }

    @Override
    public File unmarshal(String path) throws Exception {
        return new File(path);
    }

}

演示版

package forum7673323;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Recon.class);

        File xml = new File("src/forum7673323/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Recon recon = (Recon) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(recon, System.out);
    }

}

输出量

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recon toCompareFile="h:\work\two.xls" withCompareFile="h:\work\one.xls" outputPath="h:/work">
    <sheet sheetNumber="1"/>
</recon>

我可能是错的,但是getAttributes()方法不是负责带来标签的服装而是不是子元素的责任吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM