Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中读取xml文件的内容_Java_Xml - Fatal编程技术网

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

如何在Java中读取xml文件的内容,java,xml,Java,Xml,我创建了一个XML文件和DTD,可以在网站上找到 我已经写了一个代码,但它工作到一个级别,然后它就不能正常工作。我还创建了某些对象来存储xml文件的值。但我只能遍历xml的sheet标记,这样它就不能正常工作 Recon recon = new Recon(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocu

我创建了一个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);
  }
}
Recon-Recon=new-Recon();
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(configFile);
doc.getDocumentElement().normalize();
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
字符串outputPath=doc.getDocumentElement().getAttribute(“outputPath”);
字符串withCompareFilePath=doc.getDocumentElement().getAttribute(“withCompareFile”);
字符串toCompareFilePath=doc.getDocumentElement().getAttribute(“toCompareFile”);
侦察设置输出路径(输出路径);
recon.setToCompareFile(新文件(toCompareFilePath));
recon.setWithCompareFile(新文件(withCompareFilePath));
NodeList sheet NodeList=doc.getElementsByTagName(“表”);
List reconSheets=新建ArrayList();
对于(int i=0;i
我可能错了,但是
getAttributes()
方法不是负责带来标记的属性而不是子元素吗?

我可能错了,但是
getAttributes()不是吗
负责将标签的姿态而非子元素带入的方法?

注意:我是专家组的负责人和成员

可以使用JAXB实现将XML直接映射到域模型。JAXB需要JavaSE5,JavaSE6中包含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;

}
文件适配器

由于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 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;
}
<?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>

注意:我是专家组的负责人和成员

可以使用JAXB实现将XML直接映射到域模型。JAXB需要JavaSE5,JavaSE6中包含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;

}
文件适配器

由于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 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;
}
<?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>


@oers-感谢您进行了必要的编辑。M.J,下次请将您的代码示例包含在问题本身中。@oers-感谢您做了非常需要的编辑。M.J,下次请在问题本身中包含您的代码示例。您是否检查了包含XML文件的链接,因为我还需要从XML中读取属性,这就是我使用
getAttribute()
方法的原因。您是否检查了包含XML文件的链接,因此,我还需要从XML中读取属性,这就是我使用
getAttribute()
方法的原因。