-2

我有这个问题。我有一个 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>。我如何从结果页面中的显示中逃脱?

4

2 回答 2

1

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

另一方面,属性不支持可空类型。我个人的做法是根本不放置属性。在 XPath 表达式\\Element[not(@value)]中查找没有value属性的元素。这使您可以区分空字符串和空字符串。

于 2012-05-09T04:33:55.453 回答
0

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

  1. 遇到空值时省略输出标签和内容
  2. 在您的 XML 模式中采用一些约定来表示空值。
于 2012-05-09T04:34:18.083 回答