-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to parse XML if format in below manner? #11
Comments
Hi
Can you share example output of JSON you want to convert?
|
Thanks for the quick response Something like the below one
But as far as the example XML given in previous post so only the method sampleController should be included and tryItOut should be excluded (because covered is 0 ) in the resultant json So as per above XML the generated output should be
|
you should first define DSM mapping yaml as like your JSONoutput. result:
path: /
type: object
fields:
Name:
default:
value: JSON Outut
atStart: true
Methods:
type: array # methods are array
path: package/class/method # loop over method tag
filter: self.data.covered!=0
fields:
Id:
default: $new("java.util.Random").nextInt(10000)
Name:
path: name
xml:
attribute: true
Package:
path: name
parentPath: /package # start from root
xml:
attribute: true
Class:
path: name
parentPath: /package/class start from root
xml:
attribute: true
covered:
path: covered # name of the attribute
parentPath: counter # path which has attribute
xml:
attribute: true
Here is the code to convert your xml to json. public static void main(String[] args) throws JsonProcessingException
{
DSM dsm = new DSMBuilder(Main.class.getClassLoader().getResourceAsStream("test.yaml")).setType(TYPE.XML)
.create();
Object result = dsm.toObject(Main.class.getClassLoader().getResourceAsStream("test.xml"));
// convert to json
ObjectMapper objectMapper = dsm.getObjectMapper();
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
System.out.println(json);
} Here is the result {
"Name" : "JSON Outut",
"Methods" : [ {
"Package" : "com/example/configure",
"Name" : "sampleController",
"covered" : "23",
"Id" : 6036
} ]
} |
Awesome DSM project is very great ! |
@zszazi can't see last comment you write. I received via email but it is not listed here. |
@mfatihercik REPOST
But when I do so with using DSM the covered output is from the METHOD type instead of INSTRUCTION type (ie I get covered as 1 instead of getting 3)
PS:I am seeing major improvements in efficiency while using DSM |
type:
path: type # name of the attribute
parentPath: counter # path which has attribute
xml:
attribute: true
covered:
path: covered
filter: self.data.type=="INSTRUCTION"
parentPath: counter # path which has attribute
xml:
attribute: true
Methods:
type: array # methods are array
path: package/class/method # loop over method tag
filter: self.data.covered!=0 and self.data.Name.matches(".*Controller.*")
# Name is a String so we can use any method exist on String class.
fields:
Id:
default: $new("java.util.Random").nextInt(10000)
Name:
path: name
xml:
attribute: true
Package:
path: name
parentPath: /package # start from root
xml:
attribute: true
Class:
path: name
parentPath: /package/class start from root
xml:
attribute: true
type:
path: type # name of the attribute
parentPath: counter # path which has attribute
xml:
attribute: true
covered:
path: covered
filter: self.data.type=="INSTRUCTION"
parentPath: counter # path which has attribute
xml:
attribute: true |
@mfatihercik
Consider I have my xml file in below format (its a partial file)
I have to loop through multiple packages which can have multiple class names and which in-turn can have multiple methods inside the class
using DSM how should I capture the package name , class name and method name only if covered !=0
and filter the XML document and convert it to JSON output format
PS:I am new to java and the examples I saw on your website have data inserted between
<class>"ClassName"</class>
but not as<class name = "ClassName">...</class>
The text was updated successfully, but these errors were encountered: