Below are the xml file
<maindata>
<publication-reference>
<document-id document-id-type="docdb">
<country>US</country>
<doc-number>9820394ASD</doc-number>
<date>20111101</date>
</document-id>
<document-id document-id-type="docmain">
<doc-number>9820394</doc-number>
<date>20111101</date>
</document-id>
</publication-reference>
</maindata>
i want to extract the <doc-number>
tag value under the type = "docmain
"
below is my java code, while executed its extract 9829394ASD
instead of 9820394
public static void main(String[] args) {
String filePath ="D:/bs.xml";
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nodeList = doc.getElementsByTagName("publication-reference");
List<Biblio> docList = new ArrayList<Biblio>();
for (int i = 0; i < nodeList.getLength(); i++) {
docList.add(getdoc(nodeList.item(i)));
}
} catch (SAXException | ParserConfigurationException | IOException e1) {
e1.printStackTrace();
}
}
private static Biblio getdoc(Node node) {
Biblio bib = new Biblio();
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
bib.setCountry(getTagValue("country",element));
bib.setDocnumber(getTagValue("doc-number",element));
bib.setDate(getTagValue("date",element));
}
return bib;
}
let me know how can we check the Type its docmain or doctype, should extract only if the type is docmain else should leave the element
added the getTagValue method
private static String getTagValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodeList.item(0);
return node.getNodeValue();
}