繁体   English   中英

Java XML解析异常

[英]Java XML Parsing Unexpected

我正在尝试使用java解析普通的xml文件,并尝试将信息存储在Map中。 但是,我无法使其表现出预期的效果。

请指出引起此问题的问题。

XML档案:

<?xml version="1.0"?>
    <allnews>
    <news>
        <id>1</id>
        <title>I am news 1</title>
        <date>04-01-2018</date>
    </news>
    <news>
        <id>2</id>
        <title>I am news 2</title>
        <date>04-01-2018</date>
    </news>
    </allnews>

Java文件:

public class readxml {
    public static void main(String[] args) {
        try {
            File file = new File("D:/Aadil/CAPS_Workspace/samp/src/news.xml");
            DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = dBuilder.parse(file);
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            Map<String, String> news = new HashMap<>();
            if (doc.hasChildNodes()) {
                news = printNote(doc.getChildNodes());
            }

            for (Map.Entry<String, String> entries : news.entrySet()) {
                System.out.println(entries.getKey() + entries.getValue());
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static Map<String, String> printNote(NodeList nodeList) {
        String newsTitle = "", newsDate = "";
        Map<String, String> data = new HashMap<>();
        try {
            for (int count = 0; count < nodeList.getLength(); count++) {
                Node tempNode = nodeList.item(count);

                if (tempNode.getNodeName().equalsIgnoreCase("title"))
                    newsTitle = tempNode.getTextContent();
                else if (tempNode.getNodeName().equalsIgnoreCase("date"))
                    newsDate = tempNode.getTextContent();

                data.put(newsDate, newsTitle);

                if (tempNode.hasChildNodes()) {
                    printNote(tempNode.getChildNodes());
                }
            }
            for (Map.Entry<String, String> entries : data.entrySet()) {
                System.out.println(entries.getKey() + entries.getValue());
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return data;
    }
    }

在执行上面的代码时,我得到以下结果:

Root element :allnews



I am news 1  
04-01-2018I am news 1  



I am news 2  
04-01-2018I am news 2 

所有不必要的空间也都放入地图中,我希望仅当有数据时才填充它。

在打印data映射的内容时,我得到以下信息:

{=}  
{=}  
{=}  
{=I am news 1, 04-01-2018=I am news 1}  
{=}  
{=}  
{=}  
{=I am news 2, 04-01-2018=I am news 2}  
{=}  
{=} 

嘿,请尝试一下:

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLParsin {
public static void main(String[] args) {
    try {
        File file = new File("Your Example");
        DocumentBuilder dBuilder = 
        DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = dBuilder.parse(file);
        System.out.println("Root element :" + d doc.getDocumentElement().getNodeName());

        Map<String, String> news = new HashMap<>();
        if (doc.hasChildNodes()) {
            news = printNote(doc.getChildNodes());
        }


        for (Map.Entry<String, String> entries : news.entrySet()) {
            System.out.println(entries.getKey() + entries.getValue());
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

private static Map<String, String> printNote(NodeList nodeList) {
    String newsTitle = "", newsDate = "";
    Map<String, String> data = new HashMap<>();
    try {
        for (int count = 0; count < nodeList.getLength(); count++) {
            Node tempNode = nodeList.item(count);

            if (tempNode.getNodeName().equalsIgnoreCase("title"))
                newsTitle = tempNode.getTextContent();
            if (tempNode.getNodeName().equalsIgnoreCase("date"))
                newsDate = tempNode.getTextContent();


            if (tempNode.hasChildNodes()) {
                printNote(tempNode.getChildNodes());
            }
        }
        if(!newsDate.isEmpty() && !newsTitle.isEmpty()){
            data.put(newsDate, newsTitle);
        }
        for (Map.Entry<String, String> entries : data.entrySet()) {
            System.out.println(entries.getKey() + entries.getValue());
        }
    } catch (Exception e) {
        System.out.println(e);
    }


    return data;
}

}

要点是,您应该再添加一个if部分,以检查您的信息是否为空。 您只想检索不为空的数据。

我希望这是您想要的解决方案,输出为:

Root element :allnews
04-01-2018I am news 1
04-01-2018I am news 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM