我一直在尝试解析一个元素,但没有成功。当我运行这段代码时,我的调试打印方法打印出“null”,这意味着找不到该元素。
数组发送是一个句子列表。每个模式都是一个正则表达式模式。我想要做的是搜索一个句子是否匹配一个模式。如果是,请从多个模板中选择一个模板,否则创建一个新条目。我没有实现新条目的创建,因为我无法让下面的代码工作,
顺便说一句,节点的内容似乎为空,而不是节点本身。
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[i]);
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());
}
}
变量解析器被分配给一个类 XMLparser。XMLparser 的代码如下:
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(); }
}
XML 文件是这样的:
<?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>
和 dtd 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT categories (category*)>
<!ELEMENT category (pattern, template*)>
<!ELEMENT pattern (#PCDATA)>
<!ELEMENT template (#PCDATA)>