I have the following problem. I want to move a nodes and all their children one level up, example:
Original XML file:
<a>
<r>
<b>test</b>
<c>
<d>test</d>
<e>test</e>
<f>
<k>ddd</k>
</f>
</c>
</r>
</a>
So I want to get the tag <r> and all its sub-elements and move them one level up:
<r>
<b>test</b>
<c>
<d>test</d>
<e>test</e>
<f>
<k>ddd</k>
</f>
</c>
</r>
I did not find any function for that in JDOM2.
The root element is a bit special, try something like this:
document.setRootElement(document.getRootElement().getChild("r").detach());
The detach is necessary to detach the r element from its current parent (the a) so it can then be re-parented to be a direct child of the Document node.
Related
assume the following XML structure:
<A>
<B>
<C>
<!-- other stuff -->
</C>
</B>
</A>
I am not interested in mapping tags A and B, I just want to map C and what is within it. I tried it like this:
#Root
#Path("/A/B")
public class C {
// Mapping for fields within
}
Also tried #Path("/A/B/C") but nothing seems to work. I don't want to create mappings for the outer tags.
It crashes with the following error message:
"Exception in thread "main" org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy #org.simpleframework.xml.Element(name=, type=void, data=false, required=true) on field 'InvoiceNumber' public java.lang.String silc.xml.ups.Invoice.InvoiceNumber for class silc.xml.ups.Invoice at line 2"
Here, "InvoiceNumber" is another element within tag C. If I comment out A and B everything works as intended with no error.
My expected result is, that tags A and B are ignored and C is treatet as root node for parsing the XML document.
Any suggestions on how to do this?
<main>
<root>
<SubRoot>
<type>R</type>
<mand>N</mand>
<Section>B</Section>
</SubRoot>
<SubRoot>
<type>P</type>
<mand>Y</mand>
<Section>A</Section>
</SubRoot>
</root>
</main>
I have above XML file on this file How i can identify that type R or any other element belongs to which node,here it's in first SubRoot node.
In a xsl file i am calling a template if section have value as A and this section is in second SubRoot node as below,
Now i want to access the some other value from second SubRoot node inside the when condition.How can i do that?
<xsl:choose>
<xsl:when test="(/main/root/SubRoot[Section = 'A'])">
//Call some template
</xsl:when>
<xsl:otherwise>
//some template
</xsl:otherwise>
</xsl:choose>
I need to verify it dynamically on XSL.Because i am not aware at run time how many nodes will get generated for the XML.
If Section element having value as A then its inside second SubRoot node.On this basis how can i access the value of other element from second SubRoot Node.Please also help to tell me the performance issue with the suggested approach.
Any Idea Suggestion must be appreciated.
In your example you are not testing whether something is in a SubRoot with Section A, but you are testing whether a SubRoot with Section A exists, that's a huge difference.
I guess what you need is something like parent::SubRoot[Section/text() = 'A'] which will test whether the direct parent tag is a SubRoot that has a Section with text A.
Or maybe something like ancestor::SubRoot[Section/text() = 'A'] which will test whether any parent, great-parent, and-so-on tag is a SubRoot that has a Section with text A.
Or maybe something like ancestor-or-self::SubRoot[Section/text() = 'A'] which will test whether the node itself or any parent, great-parent, and-so-on tag is a SubRoot that has a Section with text A.
I have an XML with attributes and elements/tags.
I want to know whether using an attribute or a tag is good according to performance.
Could you please give an example to compare if the content has a child tag and also if the content has a attribute.
My question is, is it possible to compare 2 attributes with same name in 2 different XML files and also here we will have huge data.
So, I want to be sure how the performance is, if i consider it as a attribute or tag.
<A Name="HRMS">
<B BName="IN">
<C Code="0001">
<IN irec="200" />
<OUT orec="230" Number="" Outname=""/>
</C>
<C Code="0004">
<IN irec="209" />
<OUT orec="209" Number="" Outname=""/>
</C>
<C Code="0008">
<IN irec="250" />
<OUT orec="250" Number="" Outname=""/>
</C>
</B>
</A>
Here, i have to compare irec with orec for a particular B name and C code
It's possible. You need a java lib like jsoup to help parse xml by path expression like jquery css selection expression.
Jsoup is a HTML parser, but html is a kind of xml application, so you can use it to parse xml content.
jsoup example:
String xml = "<root><person name=\"Bob\"><age>20</age></person></root>";
Document root = Jsoup.parse(xml);
System.out.println(root.body().html());//origin XML content
Elements persons = root.getElementsByTag("person");
Element person = persons.first();
System.out.println("The attribute 'name' of Person:" + person.attr("name"));
System.out.println(persons.select("person[name=Bob]").first().text());
You can implement compare difference function using jsoup simplily.
i use Java and I have 2 xml files like
<xml>
<a value="5">
<b value="7">
<c>
<d value="9">
</c>
<xml>
and
<xml>
<c>
<d value="8">
</c>
<xml>
So what i want is for every node in second xml if there exists with same node path in first xml, replace the first xml's node with the second xml's node.For these xmls, i expect
<xml>
<a value="5">
<b value="7">
<c>
<d value="8">
</c>
<xml>
Many Thanks for Your Help
You can use a Sax parser and iterate through second XML and get all the available nodes. Or use DOM for that. Same way get all nodes in first XML. Then write a logic to find the matching nodes. Then use DOM to edit first XML. Try it your self so that you can learn. See how to edit XML here
there man ways to read xml and write xml like DOM parser , jaxB so i would prefer to use JAXB marshall and unmarshaller so that you can have an object of your xml file and to set the value and get the value becomes easier
Some API returns me XmlCursor pointing on root of XML Document. I need to insert all of this into another org.w3c.DOM represented document.
At start:
XmlCursor poiting on
<a>
<b>
some text
</b>
</a>
DOM Document:
<foo>
</foo>
At the end I want to have original DOM document changed like this:
<foo>
<someOtherInsertedElement>
<a>
<b>
some text
</b>
</a>
</someOtherInsertedElement>
</foo>
NOTE: document.importNode(cursor.getDomNode()) doesn't work - Exception is thrown: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation.
Try something like this:
Node originalNode = cursor.getDomNode();
Node importNode = document.importNode(originalNode.getFirstChild());
Node otherNode = document.createElement("someOtherInsertedElement");
otherNode.appendChild(importNode);
document.appendChild(otherNode);
So in other words:
Get the DOM Node from the cursor. In this case, it's a DOMDocument, so do getFirstChild() to get the root node.
Import it into the DOMDocument.
Do other stuff with the DOMDocument.
Append the imported node to the right Node.
The reason to import is that a node always "belongs" to a given DOMDocument. Just adding the original node would cause exceptions.
I was having the same issue.
This was failing:
Node importNode = document.importNode(originalNode);
This fixed the problem:
Node importNode = document.importNode(originalNode.getFirstChild());