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?
My expected output is in this format of JAXB.
<?xml version="1.0" encoding="utf-8"?>
<req:ShipmentRequest xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" schemaVersion="5.0">
<Request>
<ServiceHeader>
<MessageTime>2001-12-17T09:30:47-05:00</MessageTime>
<MessageReference>1234567890123456789012345678901</MessageReference>
<SiteID>CustomerTest</SiteID>
<Password>alkd89nBV</Password>
</ServiceHeader>
</req:ShipmentRequest>
but i am able to get only
<?xml version="1.0" encoding="utf-8"?>
<ShipmentRequest xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" schemaVersion="5.0">
<Request>
<ServiceHeader>
<MessageTime>2001-12-17T09:30:47-05:00</MessageTime>
<MessageReference>1234567890123456789012345678901</MessageReference>
<SiteID>CustomerTest</SiteID>
<Password>alkd89nBV</Password>
</ServiceHeader>
</ShipmentRequest>
how can i add a prefix like "req:" as shown in the expected output as i am using jaxb to marshall and unmarshall
How can I save types of class fields after marshalling without using of annotation. How can I edit it without annotations? What's the best way so it looks like in the example below?
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:o xmlns:ns2="info.source4code.jaxb.model">
<test>aaaa</test>
<intTest>100</intTest>
<abc>2</abc>
</ns2:o>
Wanted output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:o xmlns:ns2="info.source4code.jaxb.model">
<test type="String">aaaa</test>
<intTest type="Integer">100</intTest>
<abc type="Integer">2</abc>
</ns2:o>
How to parse xml file, so that it would write the same nodes to in another file several times, one after the other?
For example:
1.Xml file to be parsed:
<?xml version="1.0" encoding="UTF-8"?>
<ALL>
<VER>7.0</VER>
<NATIONALITY>FIN</NATIONALITY>
<DATA>
<USER>ED</USER>
<PLZ>XXX</PLZ>
<BEGIN>2015-05-16</BEGIN>
<CURRENCY>90</CURRENCY>
<MARKE>KIA</MARKE>
<DATA>
</ALL>
2.As a result, I want to do so that was saved in another xml file, several times:
<?xml version="1.0" encoding="UTF-8"?>
<ALL>
<VER>7.0</VER>
<NATIONALITY>FIN</NATIONALITY>
<DATA>
<USER>ED</USER>
<PLZ>XXX</PLZ>
<BEGIN>2015-05-16</BEGIN>
<CURRENCY>90</CURRENCY>
<MARKE>KIA</MARKE>
<DATA>
</ALL>
<ALL>
<VER>7.0</VER>
<NATIONALITY>FIN</NATIONALITY>
<DATA>
<USER>ED</USER>
<PLZ>XXX</PLZ>
<BEGIN>2015-05-16</BEGIN>
<CURRENCY>90</CURRENCY>
<MARKE>KIA</MARKE>
<DATA>
</ALL>
...
This is my previous question: Copy nodes in the same output xml file -java
The second example is not an XML file.
Add an external tag and appendChild to the root and save.
The file will remain valid then.
<?xml version="1.0" encoding="UTF-8"?>
<ALLs>
<ALL>
<VER>7.0</VER>
<NATIONALITY>FIN</NATIONALITY>
<DATA>
<USER>ED</USER>
<PLZ>XXX</PLZ>
<BEGIN>2015-05-16</BEGIN>
<CURRENCY>90</CURRENCY>
<MARKE>KIA</MARKE>
<DATA>
</ALL>
</ALLs>
You may use random access files, jump to the end, position back before the write the new XML snippet and write again the ALLs. This should be fast.
Another option, which is useful with streams, to write the XML header always.
In this case you have to parse one-by-one, taking care that the InputStream what you pass for parsing does not close the inputStream (make your own subclass with a close that, indeed, does not close the file, and close externally; otherwise the XML parser will close the file).
<?xml version="1.0" encoding="UTF-8"?>
<ALL>
<VER>7.0</VER>
<NATIONALITY>FIN</NATIONALITY>
<DATA>
<USER>ED</USER>
<PLZ>XXX</PLZ>
<BEGIN>2015-05-16</BEGIN>
<CURRENCY>90</CURRENCY>
<MARKE>KIA</MARKE>
<DATA>
</ALL>
<?xml version="1.0" encoding="UTF-8"?>
<ALL>
<VER>7.0</VER>
<NATIONALITY>FIN</NATIONALITY>
<DATA>
<USER>ED</USER>
<PLZ>XXX</PLZ>
<BEGIN>2015-05-16</BEGIN>
<CURRENCY>90</CURRENCY>
<MARKE>KIA</MARKE>
<DATA>
</ALL>
Another option to use, e.g. JSON; convert your XML to JSON and append to a file; when reading, read line by line and convert the JSON to XML as you wish
Since your output doesn't contain a single root tag, it won't be valid XML. It sounds like you just want to output first line of file and then repeat subsequent lines an arbitrary number of times. If that's the case, you don't really care about fact that input file is [valid] XML. You could try something like the following:
final int cnt = 2;
Files.write(
Paths.get("out.txt"),
new AbstractList<String>() {
private final List<String> mStrs = Files.readAllLines(Paths.get("in.xml"));
public int size() {return 1 + (mStrs.size() - 1) * cnt;}
public String get(int pIdx) {
return
pIdx == 0 ?
mStrs.get(0) :
mStrs.get((pIdx - 1) % (mStrs.size() - 1) + 1);
}
}
);
Note that this solution assumes that input XML header is present.
I've got a XML-document with this content:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="/Stylesheet.xsl" ?>
How can I find out using ANT's <xmltask> whether the <?xml-stylesheet ?> node already exists?
That's a special node type called processing instruction.
You can query it using XPath to find out if it exists and what its attributes are:
/processing-instruction('xml-stylesheet')