解析xml节点 - 删除空节点

时间:2018-04-04 19:39:10

标签: java

我有以下xml:

<?xml version ='1.0'?>
<cjischarges>    
<cjischarge>
  <cjisid>1</cjisid>
  <chargeid>1</chargeid>
  <cjiscode>10000</cjiscode>
  <class></class>
  <cjisclass>1 0000</cjisclass>
  <describe35>VESSEL-CONCEAL NAME/NUMBER</describe35>
  <describe70>VESSEL-CONCEAL NAME/NUMBER</describe70>      
  <description>did operate and use a [boat/vessel] on the navigable waters  Custom Service] was knowingly [concealed/covered/defaced].</description>      
</cjischarge>    
<cjischarge>
  <cjisid>1220</cjisid>
  <chargeid>5762</chargeid>
  <cjiscode>10000</cjiscode>
  <class>A</class>
  <cjisclass>1A0000</cjisclass>
  <describe35>ATT-VESSEL-CONCEAL NAME/NUMBER</describe35>
  <describe70>ATT-VESSEL-CONCEAL NAME/NUMBER</describe70>      
  <description>did attempt to operate and use a [boat/vessel] on the [concealed/covered/defaced].</description>      
</cjischarge>    
<cjischarge>
  <cjisid>1280</cjisid>
  <chargeid>5822</chargeid>
  <cjiscode>10000</cjiscode>
  <class>C</class>
  <cjisclass>1C0000</cjisclass>
  <describe35>CON-VESSEL-CONCEAL NAME/NUMBER</describe35>
  <describe70>CON-VESSEL-CONCEAL NAME/NUMBER</describe70>      
  <description>did conspire with ____ (name) to operate and use a [concealed/covered/defaced].</description>      
</cjischarge>    
<cjischarge>
  <cjisid>2</cjisid>
  <chargeid>2</chargeid>
  <cjiscode>10001</cjiscode>
  <class></class>
  <cjisclass>1 0001</cjisclass>
  <describe35>IMPERSONATING AN OFFICER</describe35>
  <describe70>IMPERSONATING AN OFFICER</describe70>      
  <description></description>      
</cjischarge>
</cjischarges>

我正在尝试解析节点 - 仅显示“description”节点不为空的位置。

我尝试过以下操作,但仍然会获得空的“描述”字段。

try {
    ArrayList<HashMap<String, String>> userList = new ArrayList<>();
    ListView lv = findViewById(R.id.listview);
    InputStream istream = new FileInputStream(folder + xmlFileName);
    Log.d(TAG, "Parse File: " + folder + xmlFileName);
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(istream);
    NodeList nList = doc.getElementsByTagName("cjischarge");
    for (int i = 0; i < nList.getLength(); i++) {
        if (nList.item(0).getNodeType() == Node.ELEMENT_NODE) {
            HashMap<String, String> user = new HashMap<>();
            Element elm = (Element) nList.item(i);
            // previously: if (!getNodeValue("description", elm).isEmpty()); {
            if (!getNodeValue("description", elm).isEmpty())
                ;
            {
                user.put("cjisclass", getNodeValue("cjisclass", elm) + " - "
                            + getNodeValue("describe70", elm));
                // user.put("describe70", getNodeValue("describe70",elm));
                user.put("description", getNodeValue("description", elm));
                userList.add(user);
            }
        }
    }
} // ...

请告知建议删除“描述”字段为空的节点。

0 个答案:

没有答案