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
Related
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 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.
How to use JAVA DOM XML PARSER with an XML structure like this, can anybody give me an example, I have to do it in 3 hours.
the document looks sth like this:
<mainNode>
<header>
<a></a>
<b></b>
</header>
<part1>
<inside>
<1>text1</1>
<2>text2</2>
</inside>
</part1>
<part2>
<inside>
<1>text1</1>
<2>tex2</2>
</inside>
</part2>
</mainNode>
I need to get the 1 and two text values from a big file.
I am working on Java. I am parsing an xml file, I am getting tag values, it is working. I have xml file as follows:
<DOC>
<STUDENT>
<ID>1</ID>
<NAME>DAN</NAME>
<ADDRESS>U.K</ADDRESS>
</STUDENT>
<STUDENT>
<ID>2</ID>
<NAME>JACK</NAME>
<ADDRESS>U.S</ADDRESS>
</STUDENT>
</DOC>
I have question that I want to fetch data inside <DOC>....</DOC> with their tag name & value as well. Means I want data as follows:
"<STUDENT>
<ID>1</ID>
<NAME>DAN</NAME>
<ADDRESS>U.K</ADDRESS>
</STUDENT>
<STUDENT>
<ID>2</ID>
<NAME>JACK</NAME>
<ADDRESS>U.S</ADDRESS>
</STUDENT>"
Please guide me how to do it.
The most common approaches in Java are to use one of either SAX or Dom parsing libraries.
If you look them up you should find loads of documentation/tutorials about them.
Dom is the easiest to use normally as it stores the entire XML in memory and you cna then access any tag, however, this is less performant and can be problematic if you are using very large XML. SAX requires more work, but reads the XML and processes each tag as it gets to it.
Both are able to do what you need though.
Take a look at SAX Parser.
This link might be helpful too: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
I am developing xml editor using jsp and servlet. In this case i am using DOM parser.
I have one problem in XML editor ,
How to edit the following xml file without losing elements.
eg:
<book id="b1">
<bookbegin id="bb1">
<para id="p1">This is<b>first</b>line</para>
<para id="p2">This is<b>second</b>line</para>
<para id="p3">This is<b>third</b>line</para>
</bookbegin>
</book>
I try to edit the above xml file using dtd using jsp,servlet. but while i read the textvalue from xml, it return only first,second,third.How to read the 'This is' and 'line '. Then how to store back to the xml file using xpath.
thank in advance.
The <b> tag inside the <para> tag is another element, not a formatting tag (in XML). Therefore, you need to traverse down to it.
Like #JRL says, the <b> tags are cosnidered as well-formed XML and, as a consequence, splitted by your DOM processor.
I think youf ail to read other text elements because you only read text when an XML node has no more XML node, which is not your case here.