I need to write a program in JAVA to convert XML into JSON based on the xsd types declared in the XML. For example, if the XML string is:
<?xml version='1.0' encoding='UTF-8'?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<age xsi:type="xs:integer">11</age>
<id xsi:type="xs:string">12</id>
<name xsi:type="xs:string">JavaInterviewPoint</name>
</student>
Then the JSON will be:
{
student: {
age: 11,
id: "12",
name: "JavaInterviewPoint"
}
}
Any ideas how can I do that?
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>
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>
Greetings fellow programmers!
I am having a major problem while unmarshalling xml to an object. I need to a create a reference to an object. How can I accomplish it? I've tried to use XSL transformation but due to cycle occurence this wasnt a right approach. I am 100% sure there's an easy way out.. Here's my XML:
<report>
<subject>
<subjectId>1</subjectId>
<name>John</name>
<surname>Doe</surname>
</subject>
<subject>
<subjectId>2</subjectId>
<name>Frank</name>
<surname>Timothy</surname>
</subject>
<individual>
<individualId>10</individualId>
<name>Isaac</name>
<surname>Newton</surname>
<co-worker>
<subject>
<subjectId>1</subjectId>
<inXml>true</inXml>
</subject>
<subject>
<subjectId>2</subjectId>
<inXml>true</inXml>
</subject>
</co-worker>
</individual>
<owner>
<subject>
<subjectId>2</subjectId>
<inXml>true</inXml>
</subject>
<share>100</share>
</owner>
</report>
I need to create reference of "subject" or "individual" with inXml=true. This flag tells me wether we have got entity with that ID in the XML.
Thank you very much for your help :)
You can use #XmlID/#XmlIDREF to map key based relationships in your XML document.
Example
http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html
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.