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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/154248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:00:41  来源:igfitidea点击:

How to add an attribute to an XML node in Java 1.4

javaxml

提问by joe

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

但这会在将其转换为 Element 的行上引发 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 中不起作用。我真正需要的是使用 Element 的替代方案。我试着做

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() 不起作用。

采纳答案by joe

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

我在服务器上使用了不同的 dtd 文件。那是造成问题的原因。

回答by Garth Gilmour

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");

回答by John M

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() 并迭代以找到第一个 Element 子节点(doc root),跳过非元素节点。

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

您的 e.setAttribute() 调用看起来很合理。假设 e 是一个 Element 并且您实际上到达了那条线...

回答by Brandon DuRette

As already noted, the ClassCastExceptionis 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()返回一个DocumentType,而不是一个Element

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 Nodeand Element. Elementis 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.

看起来你很困惑NodeElementElement是 的一种实现Node,但肯定不是唯一的。所以,并不是所有Node的 都可以投射到Element。如果演员表在一台机器上工作而不是在另一台机器上工作,那是因为getMapNode()解析器的行为不同,你得到了其他东西。XML 解析器在 Java 1.4 中是可插入的,因此您可能会从不同的供应商处获得完全不同的实现,甚至会有不同的错误。

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 getElementsByTagNameor otherwise).

由于您没有发布,getMapNode()我们无法看到它在做什么,但是您应该明确说明您希望它返回哪个节点(使用getElementsByTagName或以其他方式)。