<xml-fragment>
<currentClinicalNote>
<patientFamilyHistory disorderName="CurrentCN" id="23423"/>
<patientFamilyHistory disorderName="CurrentCN1" id="23424"/>
<patientFamilyHistory disorderName="CurrentCN1" id="23424"/>
</currentClinicalNote>
</xml-fragment>
I have an XMLBean like above, now I want to replace the node[#id=23423] with a new same type node. How can I do that?
Below is sample code I tried to work..
XmlCursor xmlCursor = cursor.execQuery(nameSpace + pathExpression1);
I found the node with above code, now I have that node in cursor, How do I replace that with another?
Any replies would be appreciated.
DOM3 is not yet implemented in XML Beans,so need to work with DOM Nodes directly to fetch by finding first child in cursor.
Related
Can anyone please explain the difference between the Element object and Node object provided in JSoup ?
Which is the best thing to be used in which situation/condition.
A node is the generic name for any type of object in the DOM hierarchy.
An element is one specific type of node.
The JSoup class model reflects this:
Node
Element
Since Element extends Node anything you can do on a Node, you can do on an Element too. But Element provides additional behaviour which makes it easier to use, for example; an Element has properties such as id and class etc which make it easier to find them in a HTML document.
In most cases using Element (or one of the other subclasses of Document) will meet your needs and will be easier to code to. I suspect the only scenario in which you might need to fall back to Node is if there is a specific node type in the DOM for which JSoup does not provide a subclass of Node.
Here's an example showing the same HTML document inspection using both Node and Element:
String html = "<html><head><title>This is the head</title></head><body><p>This is the body</p></body></html>";
Document doc = Jsoup.parse(html);
Node root = doc.root();
// some content assertions, using Node
assertThat(root.childNodes().size(), is(1));
assertThat(root.childNode(0).childNodes().size(), is(2));
assertThat(root.childNode(0).childNode(0), instanceOf(Element.class));
assertThat(((Element) root.childNode(0).childNode(0)).text(), is("This is the head"));
assertThat(root.childNode(0).childNode(1), instanceOf(Element.class));
assertThat(((Element) root.childNode(0).childNode(1)).text(), is("This is the body"));
// the same content assertions, using Element
Elements head = doc.getElementsByTag("head");
assertThat(head.size(), is(1));
assertThat(head.first().text(), is("This is the head"));
Elements body = doc.getElementsByTag("body");
assertThat(body.size(), is(1));
assertThat(body.first().text(), is("This is the body"));
YMMV but I think the Element form is easier to use and much less error prone.
It's seem like same. but different.
Node have Element. and additionally have TextNode too.
so... Example.
<p>A<span>B</span></p>
In P Elements.
.childNodes() // get node list
-> A
-> <span>B</span>
.children() // get element list
-> <span>B</span>
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.
So I'm reading from an XML file with many layers of nesting in Java using xPath.
At the moment I have a method that takes the path to XML file and a xpath query as parameters, and returns a NodeIterator.
Then I iterate through those node, and for some of the nodes (if their name matches) I need to execute another query on them and get a NodeIterator of their children etc
Is it possible to have a function with 2 parameters, one an already existing Node and the other an xPath query to execute on that Node?
So replacing:NodeIterator ni = XPathAPI.selectNodeIterator(document,xpathQuery);
With some like : NodeIterator ni2 = xPathAPI.selectNodeIterator(parentNode, query);
I've searched on the internet and I can't find any examples, and I'm not sure what the syntax to do the above would be, or if it's even possible?
Many thanks in advance :)
Presumably your XPathAPI class is the Apache/Xalan org.apache.xpath.XPathAPI?
In that case, what's wrong with
static NodeIterator selectNodeIterator(Node contextNode, java.lang.String str)
It seems to do exactly what you want.
I have a JTree that I have linked with an XML Doc using DOM4J. I have a popup that allows me to add nodes where I select.
public void addNode() {
BranchTreeNode node = (BranchTreeNode) getLastSelectedPathComponent();
if ((node.getXmlNode() instanceof Element)
&& (node.getXmlNode().getName().equals(ROOT))) {
Element root = (Element) node.getXmlNode();
Element element = root.addElement(NODE);
}
}
This works great for the underlying XML, I can save it and read it perfectly. the problem is it will only add one single node in the tree and no matter how many nodes I add it will only display one, and that one's one child and so forth. Ive tried
treeModel.reload();
treeModel.reload(node);
treeModel.nodeChanged(node);
and just now, looking up the spelling in eclipse this hilariously worked
treeModel.setDocument(treeModel.getDocument());
So I guess my question now is: Is this the correct way to do it? Am I missing something?
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());