How to convert XML(attributes) file into a java(classes) file
-
I want to write a java code to read the input XML file and generate its equivalent Java code. enter code here <CodeMarkers classname="CodeGenerator" access="public"> <MethodMarker javadoc="Compares two strings" name="stringComparator" access="private" static="no" return="boolean"> <Input name="str1" notnull="true"/> <Input name="str2" notnull="false"/> <MethodVariable name="success" type="boolean" initvalue="false"/> <JavaCode> <![CDATA[ System.out.println("Executing comparator"); if (str1.equals(str2)) success = BOOLEAN.TRUE; return success; ]]> </JavaCode> </MethodMarker> <MethodMarker javadoc="Database look for an employee id" name="getEmployeeId" access="public" static="yes" return="int"> <Input name="employeeName" notnull="true"/> <MethodVariable name="empId" type="int"/> <DBInit> <SQLCode> <![CDATA[ empId = SELECT emp_id FROM test.employee where emp_id = empId; return empId; ]]> </SQLCode> </DBInit> </MethodMarker> I need to write a java code to convert the above XML code into a java code ie The first line of output java code should be public class CodeGenerator{} enter code here <![CDATA[ empId = SELECT emp_id FROM test.employee where emp_id = empId; return empId; ]]> Im not able to put the Lines under CDATA should be used as is in the method Thanks Athreya
-
Answer:
Q2: "How to get data under < ![CDATA][...]]" For retriveing the character data (CDATA) You do something like this, import org.w3c.dom.Element; public static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return ""; }
Greenhorn at Stack Overflow Visit the source
Other answers
How the class should be created form that XML is up to You. You need some kind of factory that properly parse those settings and creat a java file. That what You need is compile and load class file into you "program". Tips for compilation: http://stackoverflow.com/questions/566239/options-for-dynamic-compilation-in-java-5 This might help for loading: http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html
Vash
If I understand correcly I would: Use an XML-Parser to read in the XML-definition ( start here: http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilderFactory.html ) Use a template engine of your choise to create the source code ( may be this is a good candidate http://www.stringtemplate.org/ ) Use the java compiler to compile the generated code ( http://java.sun.com/javase/6/docs/api/javax/tools/JavaCompiler.html ) Load your generated code into the JVM ( http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html?page=3 )
Arne
Related Q & A:
- How to convert XML files to PDF files?Best solution by Super User
- How to convert xml to json in c#?Best solution by Stack Overflow
- How to convert a HTML file to XML file?Best solution by Stack Overflow
- How do you convert a windows movie maker file to a regular video file?Best solution by Yahoo! Answers
- How to convert a .DMG file into a .EXE?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.