如何在Java 1.4中向XML节点添加属性

时间:2022-08-19 19:36:01

I tried:

我试着:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
Node mapNode = getMapNode(doc);
System.out.print("\r\n elementName "+ mapNode.getNodeName());//This works fine.

Element e = (Element) mapNode; //This is where the error occurs
//it seems to work on my machine, but not on the server.
e.setAttribute("objectId", "OBJ123");

But this throws a java.lang.ClassCastException error on the line that casts it to Element. mapNode is a valid node. I already have it printing out

但是这会抛出java.lang。在将其转换为元素的行上发生ClassCastException错误。mapNode是一个有效的节点。我已经打印出来了

I think maybe this code does not work in Java 1.4. What I really need is an alternative to using Element. I tried doing

我认为这段代码在Java 1.4中可能不适用。我真正需要的是使用元素的替代品。我试着做

NamedNodeMap atts = mapNode.getAttributes();
    Attr att = doc.createAttribute("objId");
    att.setValue(docId);    
    atts.setNamedItem(att);

But getAttributes() returns null on the server. Even though its not and I am using the same document locally as on the server. And it can print out the getNodeName() its just that the getAttributes() does not work.

但是getAttributes()在服务器上返回null。尽管它不是,而且我正在本地使用与服务器上相同的文档。它可以输出getNodeName(),只是getAttributes()不工作。

4 个解决方案

#1


1  

I was using a different dtd file on the server. That was causing the issue.

我在服务器上使用了不同的dtd文件。这就是问题的起因。

#2


0  

Might the first child be a whitespace only text node or suchlike?

第一个子节点可能只是一个纯文本节点或类似的空格?

Try:

试一试:

System.out.println(doc.getFirstChild().getClass().getName());

EDIT:

编辑:

Just looked it up in my own code, you need:

只要在我自己的代码里查一下,你就需要:

doc.getDocumentElement().getChildNodes();

Or:

或者:

NodeList nodes = doc.getElementsByTagName("MyTag");

#3


0  

As already noted, the ClassCastException is probably not being thrown in setAttribute. Check the line number in the stack. My guess is that getFirstChild() is returning a DocumentType, not an Element.

如前所述,ClassCastException可能不会在setAttribute中抛出。检查堆栈中的行号。我的猜测是getFirstChild()返回的是document类型,而不是元素。

Try this:

试试这个:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);

Element e = (Element) doc.getDocumentElement().getFirstChild();
e.setAttribute("objectId", "OBJ123");

Update:

更新:

It seems like you are confusing Node and Element. Element is an implementation of Node, but certainly not the only one. So, not all Node's are castable to Element. If the cast is working on one machine and not on another, it's because you're getting something else back from getMapNode() because the parsers are behaving differently. The XML parser is pluggable in Java 1.4, so you could be getting an entirely different implementation, from a different vendor, with different bugs even.

似乎您混淆了节点和元素。元素是节点的实现,但肯定不是唯一的。因此,并不是所有节点都可以对元素进行浇注。如果转换是在一台机器上工作,而不是在另一台机器上工作,那是因为您从getMapNode()获得了一些其他内容,因为解析器的行为不同。XML解析器在Java 1.4中是可插拔的,因此您可以从不同的供应商获得完全不同的实现,甚至有不同的bug。

Since you're not posting getMapNode() we cannot see what it's doing, but you should be explicit about what node you want it to return (using getElementsByTagName or otherwise).

由于您没有发布getMapNode(),所以我们无法看到它在做什么,但是您应该清楚地了解您希望它返回的节点(使用getElementsByTagName或其他方式)。

#4


0  

I think your cast of the output of doc.getFirstChild() is where you're getting your exception -- you're getting some non-Element Node object. Does the line number on the stack trace point to that line? You might need to do a doc.getChildNodes() and iterate to find the first Element child (doc root), skipping non-Element Nodes.

我认为您对doc.getFirstChild()输出的转换就是您获得异常的地方——您将获得一些非元素节点对象。堆栈跟踪上的行号是否指向该行?您可能需要执行doc. getchildnodes()并迭代以找到第一个元素子元素(doc root),跳过非元素节点。

Your e.setAttribute() call looks sensible. Assuming e is an Element and you actually get to that line...

您的e.setAttribute()调用看起来是合理的。假设e是一个元素,你得到这条线。

#1


1  

I was using a different dtd file on the server. That was causing the issue.

我在服务器上使用了不同的dtd文件。这就是问题的起因。

#2


0  

Might the first child be a whitespace only text node or suchlike?

第一个子节点可能只是一个纯文本节点或类似的空格?

Try:

试一试:

System.out.println(doc.getFirstChild().getClass().getName());

EDIT:

编辑:

Just looked it up in my own code, you need:

只要在我自己的代码里查一下,你就需要:

doc.getDocumentElement().getChildNodes();

Or:

或者:

NodeList nodes = doc.getElementsByTagName("MyTag");

#3


0  

As already noted, the ClassCastException is probably not being thrown in setAttribute. Check the line number in the stack. My guess is that getFirstChild() is returning a DocumentType, not an Element.

如前所述,ClassCastException可能不会在setAttribute中抛出。检查堆栈中的行号。我的猜测是getFirstChild()返回的是document类型,而不是元素。

Try this:

试试这个:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);

Element e = (Element) doc.getDocumentElement().getFirstChild();
e.setAttribute("objectId", "OBJ123");

Update:

更新:

It seems like you are confusing Node and Element. Element is an implementation of Node, but certainly not the only one. So, not all Node's are castable to Element. If the cast is working on one machine and not on another, it's because you're getting something else back from getMapNode() because the parsers are behaving differently. The XML parser is pluggable in Java 1.4, so you could be getting an entirely different implementation, from a different vendor, with different bugs even.

似乎您混淆了节点和元素。元素是节点的实现,但肯定不是唯一的。因此,并不是所有节点都可以对元素进行浇注。如果转换是在一台机器上工作,而不是在另一台机器上工作,那是因为您从getMapNode()获得了一些其他内容,因为解析器的行为不同。XML解析器在Java 1.4中是可插拔的,因此您可以从不同的供应商获得完全不同的实现,甚至有不同的bug。

Since you're not posting getMapNode() we cannot see what it's doing, but you should be explicit about what node you want it to return (using getElementsByTagName or otherwise).

由于您没有发布getMapNode(),所以我们无法看到它在做什么,但是您应该清楚地了解您希望它返回的节点(使用getElementsByTagName或其他方式)。

#4


0  

I think your cast of the output of doc.getFirstChild() is where you're getting your exception -- you're getting some non-Element Node object. Does the line number on the stack trace point to that line? You might need to do a doc.getChildNodes() and iterate to find the first Element child (doc root), skipping non-Element Nodes.

我认为您对doc.getFirstChild()输出的转换就是您获得异常的地方——您将获得一些非元素节点对象。堆栈跟踪上的行号是否指向该行?您可能需要执行doc. getchildnodes()并迭代以找到第一个元素子元素(doc root),跳过非元素节点。

Your e.setAttribute() call looks sensible. Assuming e is an Element and you actually get to that line...

您的e.setAttribute()调用看起来是合理的。假设e是一个元素,你得到这条线。