• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Need help parsing XML based text to java code

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I'm new here Have a bit of a problem at hand which seems to be getting worse as I delve deeper.

I have something like:

<abc xsi:type:ClassA>.<xyz xsi:type:ClassB>.<def>

<abc xsi:type:ClassA>.<source xsi:type:ClassC>.<ghi xsi:type:ClassD>.<jkl xsi:type:ClassE>.<def>


and I want to convert it into:

private static ClassA getAbc(){
ClassA classA=new ClassA();
classA.setXyz(getXyz());
classA.setSource(getSource());
return classA;
}

private static ClassB getXyz(){
ClassB classB=new ClassB();
classB.setDef("");
return classB;
}

private static ClassC getSource(){
ClassC classC=new ClassC();
classC.setGhi(getGhi());
return classC;
}

private static ClassD getGhi(){
ClassD classD=new ClassD();
classD.setJkl(getJkl());
return classD;
}

private static ClassE getJkl(){
ClassE classE=new ClassE();
classE.setDef("");
return classE;
}


which is basically to create a Junit. Any help or suggestions would be really helpful.

Thanks!
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like it's going to be much more trouble than it's worth. My suggestion would be to give it up.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are basically writing a compiler. That requires you to have a very strict definition of what you have, what you want, and how you get from the former to the latter.

Can you explain in English exactly how you get from one to the other? Simply providing an example or two (or even 30) is NOT a definition. It needs to be 100% deterministic as to what is allowed and what is not.
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Junilu. Don't use a format which is rather like XML, since then you have to write your own parser. Use XML, then you can use parsers which already exist.
 
Rajat Kapoor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this is my office work which we have to do manually (takes a lot of time & frustration because of hundreds of lines like those I mentioned) and I was trying to make it simpler by automating it so can't give up.
Yes it is indeed XML based but that is how we receive it from another team working on the same project so I can't change it to pure XML.

Ok let me explain in pure English if people can help:

Suppose we have:

<abc xsi:type:ClassA>.<xyz xsi:type:ClassB>.<def>

(abc,xyz,def are basically setter methods here and xsi:type following them is the return type for them)

1. I start by writing

private static <name of class after xsi:type:> get<name of tag before xsi:type: with proper camelspace naming> ( )

which gives us

private static ClassA getAbc ()

2. Under it I first make an object of ClassA

ClassA classA=new ClassA();

then I use the setter of this object to set xyz which is of type ClassB

classA.setXyz(getXyz01());

and lastly return this object

return classA;

so finally getting:

private static ClassA getAbc(){
ClassA classA=new ClassA();
classA.setXyz(getXyz());
return classA;
}

3. Now the getXyz() method needs to set def as that is the tag which comes after it. Since xyz is of xsi:type ClassB, getXyz()'s return type will be ClassB

private static ClassB getXyz(){
ClassB classB=new ClassB();
classB.setDef("any value");
return classB;
}
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so...write the code to do that. it basically would be a loop as you read the input file, a bunch of boilerplate output with a few variables from what you parse.

What part do you need help with?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic