Java XML file parsing - keeps on giving null when i attempt to get a node

markt

New Member
I've been attempting to parse an element without any success. When i run this code, my debugging print method prints out "null", which means that the element could not be found. The array sents is a list of sentences. Each pattern is a regex pattern. What I am trying to do is to search if a sentence matches a pattern. If it does, select a template out of the multiple templates, else create a new entry. I have not implemented the creation of a new entry because I could not get the code below to work,BTW, the content of the node appears to be null, not the node itself.\[code\]for (int i = 0; i < sents.length; i++) {for (int i1 = 0; i1 < categories.getLength(); i1++) {Element root = parser.document.getDocumentElement();NodeList categories = root.getChildNodes();category = (Element) categories.item(i1);pattern = category.getElementsByTagName("pattern").item(0);if (pattern != null) { System.out.println(pattern.getNodeValue());}else { System.out.println("Pattern object is null.");}Pattern regex = Pattern.compile(pattern.getNodeValue()); Matcher matcher = regex.matcher(sents); if (matcher.matches()) { // If current sentence matches matches = true; break; }}if (matches) { // If found templates = category.getElementsByTagName("template"); int chosen = Generator.generateInt(0,templates.getLength()-1); // Another class method tht generates an integer between 0 and templates.getLength()-1, note that the lower range and upper range are both inclusive. Node template = templates.item(chosen); System.out.println(template.getNodeValue());}}\[/code\]The variable parser is assigned to a class XMLparser. The code of XMLparser is as such:\[code\] public XMLparser(String name) { document = getDocument(name);}protected static Document getDocument(String name) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new InputSource(name)); } catch (Exception e) { e.printStackTrace(); } return null;}protected void saveFile(String name) { try { //write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(name+".xml")); transformer.transform(source, result); System.out.println("File saved!"); } catch (Exception e) { e.printStackTrace(); }}\[/code\]The XML file is as such:\[code\]<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE categories SYSTEM "vocab.dtd"><categories> <category> <pattern>WHAT IS YOUR NAME</pattern> <template>My name is JavaBot.</template> </category></categories>\[/code\]And the dtd file:\[code\]<?xml version="1.0" encoding="UTF-8"?><!ELEMENT categories (category*)><!ELEMENT category (pattern, template*)><!ELEMENT pattern (#PCDATA)><!ELEMENT template (#PCDATA)>\[/code\]
 
Back
Top