如何转义XML null值?

时间:2012-05-09 04:28:50

标签: java xml null escaping

我有这个问题。我有一个XML数据但不是.xml格式。

我需要根据搜索关键字显示结果,然后以xml格式返回值。但是,有些节点不包含任何值(空值)。我如何逃避这个价值?

我想显示只有值的节点(非空节点)。感谢。

这是我的代码。我是java编程的初学者。我尽可能地得到帮助。谢谢大家!

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(result));
                Document doc = db.parse(is);
                NodeList nodes = doc.getElementsByTagName("DOCUMENT");

                // iterate the DOCUMENT                
                out.println("<table border='0'>");
                int index_location=nodes.getLength()+1;
                for (int i = 0; i < nodes.getLength(); i++) {

                    locations_array+="[";

                    Element element = (Element) nodes.item(i); 

                    NodeList IDS = element.getElementsByTagName("IDS");
                    Element line = (Element) IDS.item(0);
                    out.println("<tr><td>Company ID :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList FULL_NAME = element.getElementsByTagName("FULL_NAME");
                    line = (Element) FULL_NAME.item(0);
                    out.println("<tr><td>Company Name :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");
                    locations_array+="'"+getCharacterDataFromElement(line)+"',";

                    NodeList AD_WEIGHT = element.getElementsByTagName("AD_WEIGHT");
                    line = (Element) AD_WEIGHT.item(0);
                    out.println("<tr><td>AD_WEIGHT :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList MAP = element.getElementsByTagName("MAP");
                    line = (Element) MAP.item(0);
                    out.println("<tr><td>Longitude and Latitude :</td><td>");

                    if (getCharacterDataFromElement(line)!="No data provided."){
                        String st = getCharacterDataFromElement(line);
                        String str[] = st.split(":", 2);
                        String latlong;
                        latlong = str[1];
                        out.println(latlong);
                        locations_array+="'"+latlong+"',";
                    }else{
                        out.println("No data provided.");
                        locations_array+="'0,0',";
                    }

                    out.println("</td></tr>");

                    NodeList STATE = element.getElementsByTagName("STATE");
                    line = (Element) STATE.item(0);
                    out.println("<tr><td>State :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList CITY = element.getElementsByTagName("CITY");
                    line = (Element) CITY.item(0);
                    out.println("<tr><td>City :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList ADDRESS = element.getElementsByTagName("ADDRESS");
                    line = (Element) ADDRESS.item(0);
                    out.println("<tr><td>Address :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList PHONE = element.getElementsByTagName("PHONE");
                    line = (Element) PHONE.item(0);
                    out.println("<tr><td>Phone No :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList CLASS_DESC = element.getElementsByTagName("CLASS_DESC");
                    line = (Element) CLASS_DESC.item(0);
                    out.println("<tr><td>CLASS_DESC :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList MAIN_CLASS = element.getElementsByTagName("MAIN_CLASS");
                    line = (Element) MAIN_CLASS.item(0);
                    out.println("<tr><td>Category :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList URL = element.getElementsByTagName("URL");
                    line = (Element) URL.item(0);
                    out.println("<tr><td>Website :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList EMAIL = element.getElementsByTagName("EMAIL");
                    line = (Element) EMAIL.item(0);
                    out.println("<tr><td>Email :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList DISPLAY_AD = element.getElementsByTagName("DISPLAY_AD");
                    line = (Element) DISPLAY_AD.item(0);
                    out.println("<tr><td>DISPLAY_AD :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList ADV_PATH = element.getElementsByTagName("ADV_PATH");
                    line = (Element) ADV_PATH.item(0);
                    out.println("<tr><td>ADV_PATH :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList SORT_NO = element.getElementsByTagName("SORT_NO");
                    line = (Element) SORT_NO.item(0);
                    out.println("<tr><td>SORT_NO :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    NodeList TOTAL_ADDRESS = element.getElementsByTagName("TOTAL_ADDRESS");
                    line = (Element) TOTAL_ADDRESS.item(0);
                    out.println("<tr><td>TOTAL_ADDRESS :</td><td>");
                    out.println(getCharacterDataFromElement(line));
                    out.println("</td></tr>");

                    out.println("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>");

                    index_location--;
                    locations_array+="'"+index_location+"']";
                    //locations_array+=",";                    
                }
                out.println("</table>");

这是示例xml

<DOCUMENT>
          <IDS>53850_WP</IDS>
          <FULL_NAME>xxx</FULL_NAME>
          <AD_WEIGHT>60</AD_WEIGHT>
          <MAP>1111:1.12344556,111.67890</MAP>
          <STATE>xxx xxx</STATE>
          <CITY>xxxx</CITY>
          <ADDRESS>123 xxx xxxx xxx</ADDRESS>
          <PHONE>12-12344551</PHONE>
          <CLASS_DESC>Banks</CLASS_DESC>
          <MAIN_CLASS>Banks</MAIN_CLASS>
          <URL>www.qwe.com</URL>
          <EMAIL>qwer@qwe.com</EMAIL>
          <DISPLAY_AD />
          <ADV_PATH />
          <SORT_NO>123</SORT_NO>
          <TOTAL_ADDRESS>123</TOTAL_ADDRESS>
          <INLINE_IMAGE />
          <E_PRODUCT />
          <FIELD1 />
        </DOCUMENT>

从示例中,有<INLINE_IMAGE><E_PRODUCT><FIELD1>等空节点。如何从结果页面中的显示中删除它?

2 个答案:

答案 0 :(得分:1)

最好的方法是不为它添加任何文本值。例如,如果它是一个元素,则值为<Element/>。请注意,open标签自行关闭。您可以阅读为构建模式定义(XSD)所需的nillable元素。

另一方面,

属性不支持nillable类型。就个人而言,我这样做的方法是根本不放置属性。在XPath表达式\\Element[not(@value)]中找到没有value属性的Element。这允许您区分null和空字符串。

答案 1 :(得分:0)

XML没有null的概念,也没有如何转义它。在我看来,你有两个选择:

  1. 在遇到空值时忽略输出标记和内容
  2. 在XML架构中采用一些约定来表示空值。