0

我一直在尝试解析一个元素,但没有成功。当我运行这段代码时,我的调试打印方法打印出“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)>
4

2 回答 2

0

我真的不明白你的第一段代码是如何编译的,更不用说它应该做什么了,因为你在一个循环中定义了一个由一个叫做分类的变量限制的分类,所以我一定遗漏了一些东西。但是,您的实际问题是您正在打印元素的节点值,这没什么。一个元素可以包含其他节点,但没有值。我认为您真的是要打印元素内的文本节点的值

于 2012-06-22T12:05:09.140 回答
0

尝试这样做,我从我的项目中举一个例子..这是一个单节点

Xml 请求:

<?xml version="1.0" encoding="UTF-8"?>
<request>
<token></token>
<key>xxxx</key>
<secret>xxxx</secret>
</request>

Xml 响应:

<?xml version="1.0" encoding="UTF-8"?>
   <response>
   <token>2015fc52888d2953a30550c49d548299e8f5934d</token>
   </response>

DocumentBuilder 代码:

public void getMToken(String xml){
        try {

                DocumentBuilderFactory odbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder odb =  odbf.newDocumentBuilder();
                InputSource is = new InputSource(new StringReader(xml));
                Document odoc = odb.parse(is);
                odoc.getDocumentElement().normalize ();    
                System.out.println ("Root element of the doc is " + odoc.getDocumentElement().getNodeName());
                NodeList LOP = odoc.getElementsByTagName("response");

                    Node FPN =LOP.item(0);
                    if(FPN.getNodeType() == Node.ELEMENT_NODE)
                        {

                        Element token = (Element)FPN;

                        NodeList oNameList = token.getElementsByTagName("token");
                        Element firstNameElement = (Element)oNameList.item(0);
                        NodeList textNList = firstNameElement.getChildNodes();
                        this.setToken(((Node)textNList.item(0)).getNodeValue().trim());
                        System.out.println("#####The Parsed data#####");
                        System.out.println("token : " + ((Node)textNList.item(0)).getNodeValue().trim());
                        System.out.println("#####The Parsed data#####");

                        }//end of if clause

                      }catch (SAXParseException err) {

            System.out.println (err.getMessage ());

            }catch (SAXException e) {
                 e.printStackTrace ();

            }catch (Throwable t) {
            t.printStackTrace ();
            }          
        }//end of token function
于 2012-06-22T12:07:36.430 回答