0

I am creating a KML document in Java. Inside of it i have to add many similar elements, resulting in the need to add a function, where I can pass needed arguments.

The problem is that when I try to add a part of the document into the main document it shows error, or creates malformed document. Here's a code snippet:

Element style = doc.createElement("Style");
style.setAttribute("id", "green");
dnode.appendChild(style);
Element polyStyle = doc.createElement("PolyStyle");
style.appendChild(polyStyle);
Element color = doc.createElement("color");
color.appendChild(doc.createTextNode("5014F064"));
polyStyle.appendChild(color);
Element iconStyle = doc.createElement("IconStyle");
style.appendChild(iconStyle);
color = doc.createElement("color");
color.appendChild(doc.createTextNode("5014F064"));
iconStyle.appendChild(color);

Element "dnode" is a Document element inside xml. I want to try something like this:

doc.appendChild(addFeatureStyle("red", "501400FA"));

Called three times with different parameters but have no idea how to include it. I want to add function written above, calling the code snippet.

Should the function "addFeatureStyle" return element, or a string, or something else?

1 Answer 1

1

I'm not sure I understand your question, but I'll try to answer:

Should the function "addFeatureStyle" return element, or a string, or something else?

You're calling the method appendChild() with the value returned by addFeatureStyle("red", "501400FA") as argument.

The documentation of appendChild() shows that it takes a Node as argument. So the return type of addFeatureStyle() can't be a String: String doesn't implement the Node interface. The return type of addFeatureStyle() must be Node, or a class implementing Node, or an interface extending Node.

Sign up to request clarification or add additional context in comments.

3 Comments

So with the return type of Element, should appendChild accept it or not?
See docs.oracle.com/javase/7/docs/api/org/w3c/dom/Element.html: it says: All Superinterfaces: Node So Element extends Node, so Element is a Node. So yes, of course, you can call appendChild() with an Element as argument.
Yes, when I received the Element it worked! Solved :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.