Edit:
My question is similar to the below question:
Converting XML to JSON using XML Schema
But:
With Java and not Javascript
In both directions (XML->JSON and JSON->XML)
A general Java library, not related to Node.js
Do it on-the-fly
Something like String json = convertToJSON(xml, xmlSchema)and String xml = convertToXML(json, xmlSchema)
The sax2j (schema-aware XML-to-JSON translator) library/tool seems to do the trick. It's only half of the answer since only for the XML->JSON translation, as far as I can see, but it's a start.
Related
I am writing an application/class that will take in a template text file and a JSON value and return interpolated text back to the caller.
The format of the input template text file needs to be determined. For example: my name is ${fullName}
Example of the JSON:
{"fullName": "Elon Musk"}
Expected output:
"my name is Elon Musk"
I am looking for a widely used library/formats that can accomplish this.
What format should the template text file be?
What library would support the template text file format defined above and accept JSON values?
Its easy to build my own parser but there are many edge cases that needs to be taken care of and I do not want to reinvent the wheel.
For example, if we have a slightly complex JSON object with lists, nested values etc. then I will have to think about those as well and implement it.
I have always used org.json library. Found at http://www.json.org/.
It makes it really easy to go through JSON Objects.
For example if you want to make a new object:
JSONObject person = new JSONObject();
person.put("fullName", "Elon Musk");
person.put("phoneNumber", 3811111111);
The JSON Object would look like:
{
"fullName": "Elon Musk",
"phoneNumber": 3811111111
}
It's similar to retrieving from the Object
String name = person.getString("fullName");
You can read out the file with BufferedReader and parse it as you wish.
Hopefully I helped out. :)
This is how we do it.
Map inputMap = ["fullName": "Elon Musk"]
String finalText = StrSubstitutor.replace("my name is \${fullName}", inputMap)
You can try this:
https://github.com/alibaba/fastjson
Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
I really need help to extract Mircodata which is embedded in HTML5. My purpose is to get structured data from a webpage just like this tool of google: http://www.google.com/webmasters/tools/richsnippets. I have searched a lot but there is no possible solution.
Currently, I use the any23 library but I can’t find any documentation, just only javadocs which dont provide enough information for me.
I use any23's Microdata Extractor but getting stuck at the third parameter: "org.w3c.dom.Document in". I can't parse a HTML content to be a w3cDom. I have used JTidy as well as JSoup but the DOM objects in these library are not fixed with the Extractor constructor. In addition, I also doubt about the 2nd parameter of the Microdata Extractor.
I hope that anyone can help me to do with any23 or suggest another library can solve this extraction issues.
Edit: I found solution myself by using the same way as any23 command line tool did. Here is the snippet of code:
HTTPDocumentSource doc = new HTTPDocumentSource(DefaultHTTPClient.createInitializedHTTPClient(), value);
InputStream documentInputInputStream = doc.openInputStream();
TagSoupParser tagSoupParser = new TagSoupParser(documentInputInputStream, doc.getDocumentURI());
Document document = tagSoupParser.getDOM();
ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
MicrodataParser.getMicrodataAsJSON(tagSoupParser.getDOM(),new PrintStream(byteArrayOutput));
String result = byteArrayOutput.toString("UTF-8");
These line of code only extract microdata from HTML and write them in JSON format. I tried to use MicrodataExtractor which can change the output format to others(Rdf, turtle, ...) but the input document seems to only accept XML format. It throws "Document didn't start" when I put in a HTML document.
If anyone found the way to use MicrodataExtractor, please leave the answer here.
Thank you.
xpath is generally the way to consume html or xml.
have a look at: How to read XML using XPath in Java
I am reading from an API provided by a company, but the problem is that one of the accounts from which I am getting the data has around 22000 json objects, it reads fine with small amounts of data, i would say up to 8000 records, but then I get issues like the json is not well formatted besides the problem of being able to read the response.
The response comes this way:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ywers.com">
[{"Name":"Edward", "LastName":"Jones", "Address":"{accepted}"}
,{"Name":"Carlos", "LastName":"Ramirez", "Address":"{Rejected}"}, ....... 22k more records here]</string>
I asked for some help earlier on here for the best way to do this, and i got a response about reading it using the xml parser and then a json parser, i am using GSON.
String XML = "<Your XML Response>";
XPathExpression xpath = XPathFactory.newInstance()
.newXPath().compile("/*[local-name()='string']");
String json = xpath.evaluate(new InputSource(new StringReader(XML)));
and then
JSONArray jsonRoot = new JSONArray(json.trim());
System.out.println(jsonRoot.getJSONObject(0).getString("Address")); // {accepted}
The problem with this is approach i am having is that it throws errors when reading the XML, it starts reading but after a while it stops with errors like:
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractBuilder.java:94)
at java.lang.StringBuffer.append(StringBuffer.java:219)
at org.apache.harmony.xml.dom.CharacterDataImpl.appendData(CharacterDataImpl.java:43)
......
I would appreciate any advise on how to proceed with this, I am kind of new to android.
I don't know who would wrap 22k objects inside a xml string, but apparently someone is doing that. From my experience, your out of memory is because the you try to convert all the response to string but the response is too big to be handled. I recommend you to stream the JSON data. You can do stream the JSON data from the inputstream response that you get from the your HTTP post, but you need to skip the XML part by creating another input stream from the original response input stream and skip the XML part
Before I use the streaming API from google GSON I also got OOM error because the JSON data I got is very big data (many images and sounds in Base64 encoding) but with GSON streaming I can overcome that error because it reads the data per token not all at once. And for alternative you can also use Jackson JSON library I think it also have streaming API and how to use it almost same with my implementation with google GSON. I hope my answer can help you and if you have another question about my answer feel free to ask in the comment :)
I want to parse xml data using xmlbeans and store it into objects,The following is the xml file, I want to parse it, I have model I just want to store all data into model objects,
<?xml version="1.0" encoding="UTF-8" ?>
<data:events>
<data:event>
<data:name>test</data:name>
<data:date>2013-05-20-04:00</data:date>
<data:track>
<data:name>test</data:name>
<data:race>
<data:raceNumber>1</data:raceNumber>
<data:postTimeDisplay>5:25 PM</data:postTimeDisplay>
<data:runner>
<data:programNumber>1</data:programNumber>
<data:ownerName>user</data:ownerName>
</data:runner>
<data:runner>
<data:programNumber>7</data:programNumber>
<data:ownerName>PP</data:ownerName>
</data:runner>
</data:race>
</data:track>
</data:event>
</data:events>
I tried and search lot for parsing the above data using xmlbeans but I didn't get proper solution for the same and I am quit new in xml data parsing,
Please have look this and suggest to parse this xml.
So you dont want to use SAX / DOM parsers ?
For xmlbeans did you get chance to look at
http://www.ibm.com/developerworks/library/x-beans1/
http://xmlbeans.apache.org/docs/2.0.0/guide/conGettingStartedwithXMLBeans.html
As you said you are new to parsing xml using java and If you want to use SAX/DOM go throug
http://www.youtube.com/watch?v=q-oQ5D91TAk
I am having a really challenging time parsing some XML data returned to my Android app.
The data is sent as XML but printing it on my mobile screen, it comes out as the following:
{"sessid":"5eed0b52c6953b52e262b559b5557be4","session_name":"SESS6cbf091341a26e4687fa7850b465755a,"user":{"uid":"15","name":"guest","pass":"084e0343a0486ff05530df6c705c8bb4","mail":"adeoduye#hotmail.com", "mode":"0","sort":"0","threshold":"0","theme":"","signature":"","signature_format":"0","created":"1306008217","access":"1306094503","login":"1306134979","status":"1","timezone":"3600","language":"","picture":"","init":"adeoduye#hotmail.com","data":a:1:{s:13:\"form_build_id\";s:37:\"form-49ea7a4ef10a8a2b31478696f17e8dee\";","form_build_id":"form-49ea7a4ef10a8a2b31478696f17e8dee","roles":{"2":"authenticated user","3":"guest"}}}
Can anyone please help a newbie and give me some ideas on how to parse this type of output and/or plain XML?
This isn't XML but JSON. You have to parse that string using the JSON API.
Basically you create a JSONObject by feeding the string into a JSONTokenizer. You can now query the values from the JSONObject as described in the API reference example.
The String you're seeing here is in JSON format. You can parse this in Andriod using the following library : http://code.google.com/p/google-gson/
For more info on json, checkout http://json.org.