Creating a document in Java - java

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?

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.

Related

How to know the element contain namespace(xmlns) or not?

<xxx1 xmlns="hello">
<xxx2>
<xxx3>
<name>rule_1</name>
</xxx3>
</xxx2>
</xxx1>
I select node by "//*[namespace-uri()='hello']/*[local-name()='name']"
It should get //hello:xxx1/xxx2/xxx3/name , and it does.
Now I try to get element . In reality, I don't know how much parent for <name> will get <xxx1>;
I try this code
node.getParent().getNamespaceURI() = "Hello"
and increase getParent() amount to get <xxx1>
But the first time I call <xxx3>.getNamespaceURI() it returns true.
Is the namespace inherited?
How to get the element has or not has xmlns?
Sorry for my question was not clearly.
I'm trying to get the element which is the first declared namespace "hello".
<xxx1 xmlns="hello">
<xxx2>
<xxx3>
this three node which one is contained xmlns="hello", 'cause <xxx2> and <xxx3> was not declare xmlns in the label.
Hello and Welcome to Stack Overflow!
Yes, namespaces are sort of inherited, but the terminology normally used is that, in your example, the <name> element is in the scope of the namespace declaration xmlns="hello", so the <name>element will be in the hello namespace.
With DOM4J, you can test whether an element is in a namespace or not like this:
boolean hasNamespace(Element e) {
return e.getNamespaceURI().length() > 0;
}
If the element is not in any namespace, getNamespaceURI() returns an empty string.
I guess that you want to select the <name> element, but you don't know at which level it be, i.e. how many parents it will have. You can always use this XPath expression:
Node node = doc.selectSingleNode("//*[namespace-uri() = 'foo' and local-name() = 'name']");

Update attribute value using vtd-xml

Given an xpath to an attribute and a new value, I am looking to update the attribute value to the new value.
I have followed the example here: http://vtd-xml.sourceforge.net/codeSample/cs7.html and come up with the following:
autoPilot.selectXPath(xpath);
modifier.updateToken(vtdNav.getAttrVal(vtdNav.toString(autoPilot.evalXPath())), newContent);
...my tests all pass but perhaps because I am not used to the "tokenized" way that vtd-xml works, it doesn't "feel" right so I am just looking for affirmation that I've done the correct thing.
Your code will work just fine... assume you will call modifier.output().
but it is not optimal...
This statement
modifier.updateToken(vtdNav.getAttrVal(vtdNav.toString(autoPilot.evalXPath())), newContent);
Can be written as
modifier.updateToken(autoPilot.evalXPath()+1, newContent);
Because if the attribute name has an index value of i (!=-1), then the attrinute value is always i+1... as attr val immediately follows an attr name. No conditional check is needed.

Appium : Verify relative element presence

Can you give a suggestion(please see pic) how can I check if selectIndicator is present on one block then I should choose another one. I know how to check if that element isPresent on whole page, but I need to find if it present on particular element. In my example I have Living Room chosen, and I need to check if DVR not chosen -choose that one. Any idea how can I do it? I was trying to check this way, but no luck:
WebElement element= driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");
[http://i.stack.imgur.com/F98DM.png]
If I am not getting you wrong you want to implement a self defined data structure for an appropriate solution. That could be something similar to this :
public class DVRList {
//declare components required to comprise one item
private String dvrOptionText ;
private boolean dvrOptionCheck ;
// implement setter..getter for these two
}
...in some method set the value using the logic
DVRList dvrlist = new DVRList();
WebElement parentOfBoth = driver.findElement(By.xpath("
//android.widget.RelativeLayout[1]/android.widget.R‌​elativeLayout[1]");
String text = parentOfBoth.findElementByAccessibilityId("First element").getText();
dvrlist.setdvrOptionText(text);
if(isElement(parenOfBoth.findElementByAccessibilityId("Second element"))
dvrlist.setdvrOptionCheck(true);
else dvrlist.setdvrOptionCheck(false);
and thereafter you can use these parameters accordingly.
Note : Parameters and approach are generalised and should be modified for serving the exact purpose.

Inserting nodes into an existing XML document in GWT client using XMLParser

I have an xml document (using the Document class in the XMLParser library of GWT client) with a format like follows:
<document><node id="0">content</node><node id="1">more content</node></document>
Given an ID, I need to insert a new node immediately after the node with that ID.
So far I've tried using insertBefore (as there is no insertAfter), but I must be using it incorrectly as nothing happens (apart from an UmbrellaException in the js console). I can't find any example usage via search engines.
My attempt is as follows (where n is the node I want to insert after):
Node nNext = n.getNextSibling(); //To get the next sibling to use it with insertBefore
Element newNode = doc.createElement("node");
newNode.appendChild(doc.createTextNode("new content")); //seems to work up until here
n.insertBefore(newNode, nNext); //so this line could be the problem?
insertBefore must be called on the parent node, so:
n.getParentNode().insertBefore(newNode, n.getNextSibling());

JDOM.Element.getChild(String) is returning unexpected results

According to the API at jdom.org, the semantics of getChild(String name):
This returns the first child element within this element with the given local name and belonging to no namespace. If no elements exist for the specified name and namespace, null is returned.
Therefore, if I have an XML structure like:
<?xml version="1.0" encoding="UTF-8"?>
<lvl1>
<lvl2>
<lvl3/>
</lvl2>
</lvl1>
I have a JDOM Element which is currently pointing to <lvl1>. I should be able to make the following call:
Element lvl3 = lvl1Element.getChild("lvl3");
and lvl3 should have non-null.
However, I'm finding that lvl3 is actually null. Am I missing something?
Here is a sample code snippet that should work:
import java.io.StringReader;
import org.jdom.*;
public static void main(String[] args){
Document doc = new SAXBuilder().build(new StringReader("path to file"));
Element lvl1Element = doc.getRootElement();
Element lvl3Element = lvl1Element.getChild("lvl3"); //is null. Why?
}
In order to get the functionality I was looking for, I used an Iterator from the getDescendants(ElementFilter) function from jdom.org
I then got the Element I was looking for by using code similar to the following:
Element lvl3 = lvl1.getDescendants(new ElementFilter("lvl3"));
You've just said it....
This returns the first child element
within this element with the given
local name...
Basically, on lvl1, your first child is lvl2. I haven't used JDOM to help further. My suggestion is to go to lvl2 and retrieve lvl3.
---lvl1
---lvl2(child of lvl1)
---lvl3(child of lvl2)

Categories

Resources