Serialization and Deserialization of a Tree to/from String in Java - java

I have a Tree which has multiple children. I have to serialize the tree to a string and deserialize it back from a string to a Tree. I am able to serialize the Tree to a string. I am using the following format:
(-- Begin children for an Node
)-- End children for a Node
, -- Delimiter for children for a Node
Ex: 1(11(111(1111),112),12(121,122,123),13(131))
However I am not able to deserialize the above string to a Tree in Java. Please help. Some Example code would help.
I am open to serializing the String to another format too as long as I am able to deserialize it.

If you're open to other formats (as stated) and use XStream it'll likely serialise/deserialise to XML without any further work on your part. You can further customise the serialisation as required.

How about using Jackon and serialize to JSon?

Related

Antlr parse Tree node coordinates?

I use Antlr4 4.9.2
I have a requirement to perform multiple passes of the same parse tree at different stages of my analysis. Some of the files my application handles are very large, therefore I'd like to be able to avoid keeping the parse tree in memory, and be able to regenerate a different parse tree instance each time. So far so good.
My challenge is that I need a way to (a) compare nodes and (b) quickly access nodes that works with different instances of equivalent parse trees.
For example the following pseudo-code generates two separate instances of a parse tree that represent the same file (therefore the parse trees and their nodes are equivalent)
ParseTree parseTree1 = parse(myFile, myGrammar)
ParseTree parseTree2 = parse(myFile, myGrammar)
Since myFile and myGrammar are the same, both parseTree1 and parseTree2 are equivalent, however are different instances and don't satisfy Objects.equals()
In ANTLR, how do I represent the coordinates C of a node in such a way that:
C(node1) = C(node2) if the nodes are equivalent
I can access C(parseTree1) or C(parseTree2) without having to visit the parse trees - so I can quickly position myself on the same node, for any instance of the parsetree
You can use ANTLR4's XPath implementation to directly access nodes in a given parse tree path. Here's how I get all query expressions in MySQL code, after parsing:
const expressions = XPath.findAll(tree, "/query/simpleStatement//queryExpression", this.parser);

Parse json string of the underline format to compare the values with another map

I have a JSON string that looks like:
"{\"info\":{\"length\":{\"value\":18},\"name\":{\"value\":\"ABC\"}}}"
say, length and name are attribute names
I have another map (say attributeMap) that (created from the results I retrieve from the database) map has attribute name and attribute value association stored.
I need to be able to parse the string and compare the value an attribute has in the above string with the value returned from the attributeMap. Based on those comparisons, I will need to take some decisions.
In order to do this, I should convert the above string to a format that would help make the above comparison easier and efficient. I don't think I should be writing my own parser to do this. what would a right way to do this?
You should use any JSON Parser, like GSON (Google) (Recommended for simplicity), JACKSON, the simple org.json, or any other..
Then you will get a JSONObject/JSONNode to navigate and do the comparison.
You can find a parsing example here: How to parse JSON in Java

How do I check if an XML node is a leaf node in Java?

I want to list of all the leaf nodes present in an XML document. The XML is not fixed, thus the code should work for any given XML file.
Find an XML parser. Those libraries will parse the XML String for you and build an Object Oriented tree of the XML nodes (called a DOM, which stands for Document Object Model). There should be definitely a method like getChildCount(), getChildren() or isLeaf().
Take a look here: Best XML parser for Java
If you are using the DOM:
if (!myNode.hasChildNodes())
{
// found a leaf node
}

Parsing XML like this

<item>
<RelatedPersons>
<RelatedPerson>
<Name>xy</Name>
<Title>asd</Title>
<Address>abc</Address>
</RelatedPerson>
<RelatedPerson>
<Name>xy</Name>
<Title>asd</Title>
<Address>abc</Address>
</RelatedPerson>
</RelatedPersons>
</item>
I d like to parse this data with a SAXParser. How can i do this?
I know the tutorials about SAX, and i can parsing any normal RSS, but i can't parsing this datas only.
Define your Problem: What you can probably do is create a Value Object(POJO) called Person which has the properties: name, title and address. You aim of parsing this XML would then be to create an ArrayList<Person> object. Defining a definite data structure helps you build logic around it.
Choose a Parser : You can then use a SAX Parser or an XML Pull Parser to browse through the tags: see this lin for a tutorial on DOM, SAX and XML Pull Parser in Android.
Data Population Logic: Then while Parsing, whenever you encounter a <RelatedPersons> tag, instantiate a new Person object. When you encounter the respective Properties tag, read the value and populate it in this object. When you encounter a closing </RelatedPersons> dump this Person Object in the ArrayList. Depending on the Parser you use, you will have to use appropriate methods to browse to the child node/nested nodes.(Refer the link for details)
By the time you are done parsing the last item node you will have all the values in your ArrayList.
Note that this is more of a theoretical answer; I hope it helps.

parsing an xml schema to read/list all elements

So, I wish to parse an xml schema and list all the elements along with their annotation and type. I looked at some java possibilities - the closest was XSOM. It seems like driving a truck trailer to get some milk from the neighborhood store.
I looked at JAXB, but there's no parse and list all elements against schemata.
I don't want to validate- only want to list the elements/type/annotation.
Groovy's xmlsurper is a decent parser, but can't parse XSD. Anything you know in Java,Groovy (or python)?
thank you for your time.
The SAX parser is very simple.

Categories