如何用Java读取xml文件的内容

时间:2011-10-06 10:58:50

标签: java xml

我创建了一个XML文件和DTD,可以在HERE找到。

我已经编写了一个代码,但它一直工作到一个级别,然后它无法正常工作。我还创建了一些对象来存储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);
  }
}

2 个答案:

答案 0 :(得分:1)

注意:我是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;

}

<强> ReconSheet

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;
}

<强> FileAdapter

由于JAXB实现无法直接与java.io.File对象交互,因此我们将使用JAXB适配器来处理此转换。使用Recon注释在@XmlJavaTypeAdapter类中指定了此适配器的使用:

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>

答案 1 :(得分:0)

我可能错了,但不是getAttributes()方法负责带来标签的属性而不是子元素?