2011-10-06 66 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()'方法。 –