简体   繁体   中英

java dom parser- how to check if an element is null or not

I am trying to parse an xml file in which there is a group element "patent-assignee" which contains some elements- name, address1, address2,city,state, postcode, country.

While values will always be there for "name" and "address1" the other elements may or may not have values.

I have navigated to a single patent-assignee element, and now want to check if this record has value for address2 (and other fields) or not.

Some relevant code is given below--

el_patentassignees= (Element) npassignee.item(ncount);
//now el_patentassignee has in it the content of one patent assignee element

el_assigneeaddress2= (Element) el_patentassignees.getElementsByTagName("address2").item(0);

val_assigneeaddress2= el_assigneeaddress2.getTextContent();

Iterate through all child nodes of el_assigneeaddress2 , then, if you see a Text node, take the value:

NodeList nodeList = el_assigneeaddress2.getChildNodes();
for (int i = 0; i < nodeList.getLength(), i++) {
  Node child = nodeList.item(i);
  if (child.getName().equals("#text")) { 
    val_assigneeaddress2= child.getTextContent();
    break;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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