2011-10-06 77 views
1

我已經創建了一個XML文件和DTD,可以在HERE找到。如何閱讀Java中的XML文件的內容

我已經寫了一個代碼,但它的工作,直到一個級別,然後它不能正常工作。我還創建了某些對象來存儲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); 
    } 
} 
+0

@oers:使用該適配器的使用@XmlJavaTypeAdapter註解放在Recon類指定需要編輯。 M.J,下次請將您的代碼示例包含在問題本身中。 – Perception

回答

1

備註:我是EclipseLink JAXB (MOXy)的領導和JAXB 2 (JSR-222)專家組的成員。

您可以使用JAXB實現將您的XML直接映射到您的域模型。 JAXB需要Java SE 5和JAXB實現在Java SE 6包含

偵察

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

FileAdap ter

由於JAXB實現無法直接與java.io.File對象交互,因此我們將使用JAXB適配器來處理此轉換。感謝做多 -

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> 
0

我可能是錯的,但不是getAttributes()方法負責把標籤的attibutes,而不是子元素?

+0

你檢查了包含XML文件的鏈接,因爲我還需要從XML讀取屬性,這就是爲什麼我使用'getAttribute()'方法。 –