I have two different xml files described as below and want to merge these xml files and get the expected output may be using xpath or dom parsing but not XSLT since the xmls are always not the same
XML1.xml
<personinfo>
<person>
<name><name>
<age></age>
<address>
<street></street>
<city></city>
<address>
</person>
<person>
<name><name>
<age></age>
<address>
<street></street>
<city></city>
<address>
</person>
<person>
<name><name>
<age></age>
<address>
<street></street>
<city></city>
<address>
</person>
</personinfo>
XML2.xml
<personinfo>
<person>
<name>tom<name>
<age>26</age>
<address>
<street>main street</street>
<city>washington</city>
<address>
</person>
<person>
<name>mike<name>
<age>30</age>
<address>
<street>first street</street>
<city>dallas</city>
<address>
</person>
</personinfo>
Expected.xml
<personinfo>
<person>
<name>tom<name>
<age>26</age>
<address>
<street>main street</street>
<city>washington</city>
<address>
</person>
<person>
<name>mike<name>
<age>30</age>
<address>
<street>first street</street>
<city>dallas</city>
<address>
</person>
<person>
<name><name>
<age></age>
<address>
<street></street>
<city></city>
<address>
</person>
</personinfo>
Thanks in advance ....
If you have the flexibility to create a new xml file, you can parse each of them using any parser you are comfortable with. Store the tags in a LinkedList of String LinkedLists and the tag values in a HashMap of the following type:
LinkedHashMap data= new LinkedHashMap();
You can then call the tag names from the linked lists, append the tag values from the Hash Map and write them out to a new XML file.
When I did merging of XMLs, this was the procedure I used.
Hope this helps
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.getElementsByTagName("employee");
for (int s = 0; s < nodeLst.getLength(); s++)
{
stkey=getXMLData(s,nodeLst,"id");
keylist.add(stkey);// adding integer keys to a Linked List
data.put(stkey, stkey);
data.put(stkey+"first",getXMLData(s,nodeLst,"firstname"));
data.put(stkey+"last",getXMLData(s,nodeLst,"lastname"));
data.put(stkey+"loc",getXMLData(s,nodeLst,"location"));
data.put(stkey+"occ",getXMLData(s,nodeLst,"occupation"));
}
this will get the tag values in the hash map and the tag names in the linked list. to make your work easier, you can append the type of tag to the hashmap key. For example: if my key is the Employee ID(in my case), I append "first" to it. Lets say some one has an id: 10001. his data would be stored as: 10001, then 10001first, 10001last, 10001loc,10001occ. Now, you can call each hashmap key, get the element as per appended tag name and concatenate to your xml file.
Hope this helps.
Related
I have around 100 plain xml files with repetetive static values. I would like to change them into dynamic by getting values from .properties file so that i can change that value in one place. Is it possible??
Example of current xmls I have :
<?xml version="1.0"?>
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
**desired change:**
**prop.properties.com**
bookName=Harry Potter
authorName=J K. Rowling
yearValue=2005
priceValue=29.99
<?xml version="1.0"?>
<bookstore>`enter code here`
//load properies file
<book category="children">
<title>${bookName}</title>
<author>${authorName}</author>
<year>${yearValue}</year>
<price>${priceValue}</price>
</book>
</bookstore>
My requirement is to fetch all the things in between particular tag in xml.
What are the possible ways of doing this?
for example my xml looks something like this:
<?xml version="1.0"?>
<class>
<teacher>
<subject>English</subject>
</teacher>
<student rollno="393">
<firstname>dinkar</firstname>
<lastname>kad</lastname>
<nickname>dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>jasvir</firstname>
<lastname>singh</lastname>
<nickname>jazz</nickname>
<marks>90</marks>
</student>
</class>
So how can I get the all the things in between <teacher> tag
seems like Xpath will be a good fit or you
I am trying to create a an XML with multiple elements. Below is the code i am trying.
Element root = doc.createElement("root");
doc.appendChild(root);
Element member = doc.createElement("member");
root.appendChild(member);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode("xxx"));
member.appendChild(name);
Element phone = doc.createElement("phone");
phone.appendChild(doc.createTextNode("vvvv"));
member.appendChild(phone);
Element sss = doc.createElement("somethingNew");
root.appendChild(sss);
Element nnn = doc.createElement("name1");
nnn.appendChild(doc.createTextNode("AAA"));
sss.appendChild(nnn);
Element ppp = doc.createElement("phoneEx");
ppp.appendChild(doc.createTextNode("cc"));
sss.appendChild(ppp);
And the output i am getting is
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<member>
<name>xxx</name>
<phone>vvvv</phone>
<name>xxx</name>
<phone>vvvv</phone>
</member>
<somethingNew/>
</root>
But i am expecting somthing like Below, Please help me where i am going wriong
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<member>
<name>xxx</name>
<phone>vvvv</phone>
<name>xxx</name>
<phone>vvvv</phone>
</member>
<somethingNew>
<name1>DDD</name1>
<phoneEx>CC</phoneEx>
</somethingNew>
</root>
Never Mind i could able to find the solution, I am appending to the first element all the time, That was the issue.Corrected my code
For the following xml:
<books>
<book>
<author>Peter</author>
<title>Tales from Somewhere</title>
<data>
<version>1</version>
</data>
</book>
<book>
<author>Paul</author>
<title>Tales from Nowhere</title>
<data>
<version>2</version>
</data>
</book>
</books>
How can I get the <version> value of the book author 'Paul' above, using this type of notation for building a Java XPathExpression:
//*[local-name()='books']/*
?
I used the following question as a reference:
Get first child node in XSLT using local-name()
Thanks!
This XPath will get the version of a book where there is at an author element with the value "Paul":
//book[author="Paul"]/data/version
When run against this XML:
<books>
<book>
<author>Peter</author>
<title>Tales from Somewhere</title>
<data>
<version>1</version>
</data>
</book>
<book>
<author>Paul</author>
<title>Tales from Nowhere</title>
<data>
<version>2</version>
</data>
</book>
<book>
<author>Peter</author>
<author>Paul</author>
<title>How to write a book with a friend</title>
<data>
<version>7</version>
</data>
</book>
</books>
You get this result:
<version>1</version>
<version>7</version>
I am getting the XML data that is sent as part of SOAP request using a handler in webservice before unmarshalling of the request is done.
Node root = soapMsgContext.getMessage().getSOAPBody().getFirstChild();
Is there to trim all the elements in the node? For instance in the below case -
<Person>
<Name> J i m </Name>
<Details>
<age> 1 9 </age>
<Address>
<firstName> J i mm y </firstName>
<lastName> An der son </lastName>
</Address>
</Details>
<Person>
must be changed to
<Person>
<Name>Jim</Name>
<Details>
<age>19</age>
<Address>
<firstName>Jimmy</firstName>
<lastName>Anderson</lastName>
</Address>
</Details>
<Person>
EDIT: Webservices framework being used is JAX-RPC.