【问题标题】:How to parse next node attribute after keypress (Java DOM Parser)如何在按键后解析下一个节点属性(Java DOM Parser)
【发布时间】:2013-11-16 12:33:45
【问题描述】:

目前我正在为我正在开发的游戏创建一些对话。 我想通过 XML 解析来做到这一点。

我在 system.out.println 中进行了解析,但它会打印 XML 中的所有结果。

XML 文档:

<?xml version="1.0"?>
<cutscene>
    <conversation id="1">
            <name>Rubeus Hagrid</name>
            <line>Sorry about the door..</line>
    </conversation>
    <conversation id="2">
            <name>Herman Duffeling</name>
            <line>I want you to leave immediately, this is my home!</line>
    </conversation>
        <conversation id="3">
            <name>Rubeus Hagrid</name>
            <line>Oh, shut up..</line>
        </conversation>
</cutscene>

Java 源代码:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

  public static void main(String argv[]) {

    try {

    File fXmlFile = new File("Resources/XML/conversations.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    NodeList nList = doc.getElementsByTagName("conversation");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;

            System.out.println(eElement.getAttribute("id"));
            System.out.println(eElement.getElementsByTagName("name").item(0).getTextContent() + ": ");
            System.out.println(eElement.getElementsByTagName("line").item(0).getTextContent());

            System.out.println("---------------------------");
        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
  }

}

代码打印如下:

1
Rubeus Hagrid: 
Sorry about the door..
---------------------------
2
Herman Duffeling: 
I want you to leave immediately, this is my home!
---------------------------
3
Rubeus Hagrid: 
Oh, shut up..
---------------------------

这就是我想做的:

 1
 Rubeus Hagrid: 
 Sorry about the door..
 ---------------------------

当您在此处单击一个键时(我在考虑 Enter 键),您需要查看下一个项目,因此它是数字 2。

请帮帮我!

【问题讨论】:

    标签: java xml parsing dom keypress


    【解决方案1】:

    我的建议是研究 Xpath 以及如何在 Java 中使用它。
    在一行中,XPath 是一种节点寻址语言,它允许您从 XML 文件中检索适当的节点或一组节点。

    由于您有一个 id 属性,该属性会随着对话的每个进展而增加 1,您可能希望使用基于 id 属性的 XPath 检索正确的节点。只需将 id 属性跟踪为int,每次按键都会增加该属性。然后,根据该值,获取节点。

    您的XPath 查询如下所示:

    //conversation[@id=2]  
    

    这会选择conversation 标记,其中id 属性为2
    现在,要获取名称和行,

    //conversation[@id=2]/line  
    

    //conversation[@id=2]/name  
    

    【讨论】:

    • 我认为你创建了一个功能,当你按下“Enter”然后每次按键时都会激活:id="current + 1"?
    • 我假设它是基于 GUI 的。所以你需要一个关键的听众。在命令行中,是的,您需要查找要按下的回车键。你的逻辑是对的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多