gpt4 book ai didi

Java DOM 解析器转换错误

转载 作者:行者123 更新时间:2023-11-30 07:19:16 31 4
gpt4 key购买 nike

好的,这就是一个简单的决策树,广度和深度优先的搜索程序。在我的打印树方法中,我使用与搜索方法中相同的元素转换过程,当我单独运行打印树方法时,我没有收到任何错误,当我取消注释我的搜索方法时,我在 main 中遇到异常,并且不能在深度优先和广度优先方法中都会抛出异常,但树仍然会打印。在我在方法中获得根子级之后,该异常特别来自于强制转换行。我需要新的眼光来看待这个问题,无论如何,我对在 java 中使用 xml 是全新的,所以我抓狂了。

xml:(评论说我错误地假设所有子节点都是元素,实际上我假设所有节点都是节点,而不是元素,并且它们可以转换为元素,抱歉)

<root>
<node behavior="Idle" response="">
<node behavior="" response="Use Computer"/>
<node behavior="" response="Patrol"/>
</node>
<node behavior="Incoming Projectile">
<node behavior="" response="Evade"/>
</node>
<node behavior="Combat" response="">
<node behavior="Melee" response="">
<node behavior="" response="Flee"/>
<node behavior="" response="Attack"/>
</node>
<node behavior="Ranged" response="">
<node behavior="" response="Weapon 1"/>
<node behavior="" response="Weapon 2"/>
<node behavior="" response="Weapon 3"/>
</node>
</node>
</root>

这是当我使用搜索运行打印树时程序的输出,就像我说的,如果我不使用它运行搜索,我就不会得到第一个异常:

Specify BehaviorIdle
behavior= Idle
Exception in thread "main" response= Use Computer
response= Patrol
behavior= Incoming Projectile
response= Evade
behavior= Combat
behavior= Melee
response= Flee
response= Attack
behavior= Ranged
response= Weapon 1
response= Weapon 2
response= Weapon 3
java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
at XmlTree.breadthFirst(XmlTree.java:58)
at decisiontree.main(decisiontree.java:34)

主要:

import java.util.*;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class decisiontree {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList <org.w3c.dom.Node> queue = new ArrayList<org.w3c.dom.Node>();
int dCounter = 0;
int bCounter = 0;

System.out.print("Type in path for xml file: ");
String path = scan.nextLine();

System.out.print("Specify Behavior");
String target = scan.nextLine();

scan.close();


XmlTree dTree = new XmlTree();
Document doc =dTree.xmlIn(path);


NodeList thisList = doc.getElementsByTagName("root");
Node root = thisList.item(0);

dTree.printTree(doc);

String dResponse = dTree.depthFirst(root, target, dCounter);
String bResponse = dTree.breadthFirst(root, queue, target, bCounter);

System.out.println("Behavior: "+ target +"\n"+"Depth First Response: " + dResponse +
"\n" + "Breadth First Response: "+ bResponse + "\n" + "Depth First Took "+
dCounter + " jumps"+"\n"+"Breadth First Took "+ bCounter +" jumps");

}

}

打印树:

public void printTree(Document doc){

NodeList rootList = doc.getElementsByTagName("root");
org.w3c.dom.Node root = rootList.item(0);
Element rootElement = (Element) root;
NodeList nodeList = rootElement.getElementsByTagName("node");

for(int i = 0; i < nodeList.getLength(); i++){
org.w3c.dom.Node nodes = nodeList.item(i);
Element nodeElement = (Element) nodes;
if(nodeElement.getAttribute("behavior") != ""){
if(nodeElement.getParentNode() != rootElement){
System.out.println(" behavior= " + nodeElement.getAttribute("behavior"));
}else{
System.out.println("behavior= " + nodeElement.getAttribute("behavior"));
}
}else if(nodeElement.getAttribute("behavior") == ""){
if(nodeElement.getParentNode() != rootElement.getChildNodes()){
System.out.println(" response= " + nodeElement.getAttribute("response"));
}else{
System.out.println(" response= " + nodeElement.getAttribute("response"));
}
}
}
}

深度优先:

public String depthFirst(org.w3c.dom.Node root, String target, int dCounter){

if (root.getChildNodes() == null){
return "no tree";
}
dCounter++;
NodeList nL = root.getChildNodes();
for(int i = 0;i < nL.getLength();i++){
org.w3c.dom.Node node = nL.item(i);
Element nodeElement = (Element) node; //ERROR ON THIS LINE
if(nodeElement.getAttribute("behavior") == target){
while(nodeElement.hasChildNodes()){
NodeList newNL = nodeElement.getChildNodes();
Random rand = new Random();
int x = rand.nextInt(newNL.getLength());
node = newNL.item(x);
Element newElement = (Element) node;
nodeElement = newElement;
dCounter++;
}
String response = nodeElement.getAttribute("response");
return response;
}else{
depthFirst(node, target, dCounter);
}
}

return null;
}

广度优先:

public String breadthFirst(Node root, ArrayList<org.w3c.dom.Node> q, String target, int bCounter){

if(!root.hasChildNodes()){
return "no tree";
}
bCounter++;
NodeList nL = root.getChildNodes();
for(int i = 0; i < nL.getLength(); i++){
q.add(nL.item(i));
}
Node node = q.get(0);
Element nodeElement = (Element) node; // ERROR ON THIS LINE

if(nodeElement.getAttribute("behavior") != target){
q.remove(0);
Node newNode = q.get(0);
Element newElement = (Element) newNode;
breadthFirst(newElement, q, target, bCounter);
}
if(nodeElement.getAttribute("behavior") == target){
while(nodeElement.hasChildNodes()){
NodeList newNL = node.getChildNodes();
Random rand = new Random();
int x = rand.nextInt(newNL.getLength());
Element newElement = (Element) newNL.item(x);;
nodeElement = newElement;
bCounter++;
}
String response = nodeElement.getAttribute("response");
return response;
}
return null;
}

最佳答案

查看 XML 的前两行:

<root>
<node behavior="Idle" response="">

看起来 <root> 的第一个子节点是 <node>元素,但事实并非如此。

第一个 child 实际上是 Text对象,其字符内容为"\n " (或者可能是 "\r\n " ,具体取决于 XML 文档使用哪个平台的行结尾),表示开头 <root> 之间的文本标签和第一个 <node> 的开头标签。第二个 child 是<node>元素。

最简单的解决方法是执行 if (nodes instanceof Element)在继续之前检查。如果您熟悉 XPath,我会使用它,因为代码会更干净。

警告:使用 == 比较字符串和!=最终会失败。在Java中,必须使用方法调用来比较String对象。您需要替换它:

nodeElement.getAttribute("behavior") == ""

这样:

nodeElement.getAttribute("behavior").isEmpty()

同样,您需要替换它:

nodeElement.getAttribute("behavior") != ""

这样:

!nodeElement.getAttribute("behavior").isEmpty()

一般来说,如果要将字符串与空字符串以外的任何值进行比较,则需要使用 equals 方法。例如:

nodeElement.getAttribute("behavior").equals(target)

关于Java DOM 解析器转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37860809/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com