Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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_Dom_Xml Parsing - Fatal编程技术网

Java 对一堆可选字段进行XML解析的最佳方法是什么?

Java 对一堆可选字段进行XML解析的最佳方法是什么?,java,dom,xml-parsing,Java,Dom,Xml Parsing,我试图解析一个XML文件,并将我解析的所有数据插入MySQL。但是,我不太明白如何解析带有变量字段的XML文件。 我已经试着研究了几个小时如何做到这一点。据我所知,您必须了解正在解析的数据类型。在这种情况下,我相信我会的。以下是我正在使用的DTD文件: 因此,如果我们有一个包含如下数据的XML文件(作为一个非常简单的示例): 现代数据库系统:对象模型、互操作性等。 安格尔论文:关系数据库系统的剖析 db/books/collections/Stonebraker86.html 然后我觉得我需

我试图解析一个XML文件,并将我解析的所有数据插入MySQL。但是,我不太明白如何解析带有变量字段的XML文件。 我已经试着研究了几个小时如何做到这一点。据我所知,您必须了解正在解析的数据类型。在这种情况下,我相信我会的。以下是我正在使用的DTD文件:

因此,如果我们有一个包含如下数据的XML文件(作为一个非常简单的示例):


现代数据库系统:对象模型、互操作性等。
安格尔论文:关系数据库系统的剖析
db/books/collections/Stonebraker86.html
然后我觉得我需要一堆if语句和一堆null检查来查看哪些字段不存在,因为DTD文件为每个元素指定了很多可能的字段。然而,我觉得这是一种非常糟糕的解析方法。我曾尝试在谷歌上搜索处理这种情况的示例,但所有示例似乎每个元素都有相同的字段

NodeList nl = docEle.getElementsByTagName("book");
if(nl != null && nl.getLength() > 0) {
    for(int i = 0 ; i < nl.getLength();i++) {

        //get the book element
        Element bl = (Element)nl.item(i);

        //get the book's associated fields
        String title = getTextValue(b1,"title");
        String url = getTextValue(bl,"url");
        //and so on and so fourth

        //check if null
        if (title.equals("")) {
            // don't use this as a field to insert into the database
        }
        if (url.equals("")) {
            // don't use this as a field either
        }
        //and so on..
    }
}
NodeList nl=docEle.getElementsByTagName(“book”);
如果(nl!=null&&nl.getLength()>0){
对于(int i=0;i
你能更具体地说明你想做什么吗?为什么一个具有可空字段的Java对象不能满足表示的要求?如果
可以包含(比如)30个不同的标记,并且您需要处理任何XML文件中存在的所有标记,那么您就无法避免为每种情况编写代码。你的问题不够具体,无法清楚地表明你正在努力完成什么。此外,一旦您要求XML解析器构建DOM,您就完成了解析。剩下的就是处理您想要的内容。不确定您想要做什么,但可能需要XSLT进行检查。对不起,我重新编辑了我的问题以使其更清楚。如果还有其他问题,请告诉我。可能创建您自己的数据类型来捕获您想要的元素,每个元素的状态将由您的xml元素定义,看看我之前提出的,以及整个线程
NodeList nl = docEle.getElementsByTagName("book");
if(nl != null && nl.getLength() > 0) {
    for(int i = 0 ; i < nl.getLength();i++) {

        //get the book element
        Element bl = (Element)nl.item(i);

        //get the book's associated fields
        String title = getTextValue(b1,"title");
        String url = getTextValue(bl,"url");
        //and so on and so fourth

        //check if null
        if (title.equals("")) {
            // don't use this as a field to insert into the database
        }
        if (url.equals("")) {
            // don't use this as a field either
        }
        //and so on..
    }
}