簡體   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