使用java在xml中将空值表示为空白

时间:2021-04-13 11:48:27

My Excel data values are: Name EmployeeId Address Phone Rony FBL123 Dhaka 12333333 Azam FBL321 Dhaka 67778888 Rony Chandpur 099776655 Azam 9988

我的Excel数据值是:姓名EmployeeId地址电话Rony FBL123达卡12333333 Azam FBL321达卡67778888 Rony Chandpur 099776655 Azam 9988


Here is my code to read a list of data values including null values: And convert data into xml:

这是我的代码,用于读取包含空值的数据值列表:并将数据转换为xml:

try {

    DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
    DocumentBuilder build = dFact.newDocumentBuilder();
    Document doc = build.newDocument();

    Element root = doc.createElement("dataroot");
    doc.appendChild(root);

    Element Details = doc.createElement("DATA");
    root.appendChild(Details);


    for(int i=0; i<list.size()-2; i +=3 ) {

        Element name = doc.createElement("Name");
        name.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
        Details.appendChild(name);

        Element id = doc.createElement("Empid");
        id.appendChild(doc.createTextNode(String.valueOf(list.get(i+1))));
        Details.appendChild(id);


        Element ad = doc.createElement("Add");
        ad.appendChild(doc.createTextNode(String.valueOf(list.get(i+2))));
        Details.appendChild(ad);


        Element mo = doc.createElement("Mobile");
        mo.appendChild(doc.createTextNode(String.valueOf(list.get(i+3))));
        Details.appendChild(mo);


    }

Here where i need to check the data is null or not and how to handle it.

这里我需要检查数据是否为null以及如何处理它。

1 个解决方案

#1


0  

You should ideally be doing this where you add the element in your list using add(). This part of code snippet is however NOT on your post above. Assuming list is defined as ArrayList<String> and val is of String type, you can do something like:

理想情况下,您应该使用add()在列表中添加元素。但是,此部分代码段不在您上面的帖子中。假设list被定义为ArrayList 而val是String类型,你可以这样做:

if (val == null) {
   list.add("");
}
else {
   list.add(val);
}

#1


0  

You should ideally be doing this where you add the element in your list using add(). This part of code snippet is however NOT on your post above. Assuming list is defined as ArrayList<String> and val is of String type, you can do something like:

理想情况下,您应该使用add()在列表中添加元素。但是,此部分代码段不在您上面的帖子中。假设list被定义为ArrayList 而val是String类型,你可以这样做:

if (val == null) {
   list.add("");
}
else {
   list.add(val);
}