Skip to content

Instantly share code, notes, and snippets.

@yulgit1
Created March 7, 2018 14:25
Show Gist options
  • Save yulgit1/419119d05b9f7c0c42048ca1012fe9fc to your computer and use it in GitHub Desktop.
Save yulgit1/419119d05b9f7c0c42048ca1012fe9fc to your computer and use it in GitHub Desktop.
dom xml
private String getISBN(Document doc) {
//<marc:datafield tag="020" ind1=" " ind2=" "><marc:subfield code="a">0809305186</marc:subfield></marc:datafield>
String isbn = new String();
System.out.println("doc:"+doc.getDocumentElement());
NodeList nl = doc.getElementsByTagName("marc:datafield").;
System.out.println("datafieldlen:"+nl.getLength());
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
NamedNodeMap attributes = n.getAttributes();
Node a = attributes.getNamedItem("tag");
if (a != null) {
if (a.getTextContent().equals("020")) {
NodeList nl2 = n.getChildNodes();
for (int ii = 0; ii < nl2.getLength(); ii++) {
Node n2 = nl2.item(ii);
System.out.println("here");
NamedNodeMap attributes2 = n2.getAttributes();
Node a2 = attributes2.getNamedItem("code");
if (a2 != null) {
if (a2.getTextContent().equals("a")) {
Element e = (Element) n2;
String isbn_e = e.getTextContent();
if (isbn_e.length() == 10) {
isbn = isbn_e;
}
}
}
}
}
}
}
System.out.println("ISBN:"+isbn);
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment