Generating XSL stylesheet in java (xml to xml) - java

i need a java code which makes an xsl style sheet that transforms an xml file to another xml file. but it should be dynamic.
i want to set the xsl:element names and path. and the java code should generate
me automaticly.
i have made one but if i want to change my type of xml, i need to add like 30 line code.
example from my code;
string xslelementstart = "<xsl:element name=\"" ;
string elementend="</xsl:element>";
string value="<xsl:value-of select=\"";
string name = "";(will be public and can be changed)
string path = "";(will be public and can be changed)
string end="\"\>";
string end2="\">";
if(path!="")
{
string xsl = xslelementstart+name+end2+"\n"+
value+path+end+"\n"
elementend
}
this is an example of my java code not the actual. im working with a big xml file. i want other xml files to be in my xml file format.but if i want to change my xml file (like adding another element) this code is not useful.as i said i should only set the values of my xml file and the java code should generates me. is it possible?

String concatenation is ugly, use Document Builder.
See the similar question: Create xslt files programmatically

Related

How to format a java String as JSP code and save it to file?

I have a Java string containing JSP code which I generated programmatically. I want the code in the string to be properly formatted and aligned and then save the code on disk.
I am already formatting some strings as Java code using Formatter.formatSource() method and even XML code in string can be formatted using javax.xml.transform.Transformer.
But I could not find anything similar to properly format the string as JSP code.
it likes html.
so try this:
Spanned spannedContent = Html.fromHtml(YOUR_STRING);
textView.setText(spannedContent, BufferType.SPANNABLE);

How to generate xml file using codename one api?

What I need is point 3:
Take one xsd file "testField.xsd" and parse it using "com.codename1.xml.XMLParser"
alter this xsd file and generate result
now I want to save this xsd file with name "testValues.xsd"
What i have done so far :
(1). Take one xsd file "testField.xsd" and parse it using "com.codename1.xml.XMLParser"
InputStream is = Display.getInstance().getResourceAsStream(null,
"/testField.xsd"+fileName);
Element response = xp.parse(new InputStreamReader(is));
(2). alter this xsd file and generate result
for(FormData fromDataObj : listInputs){
response.getElementById(fromDataObj.getInputId()).setAttribute("value",fromDataObj.getInputValue());
}
(3). now I want to save this xsd file with name testValues.xsd
//want to save above response as "testValues.xsd"
Look at the XmlWriter class it has the ability to write Element's to XML.

Convert a String into an XML file

I am using Castor XMLDiff to find the difference between 2 XML files. It compares two XML documents located at the given URL locations. Both my XML files are being generated at runtime and they are in the form of String. My question is how can I convert a String into an XML file, so that I can pass the file location as an argument.
I have a String in the following form:
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Header><MessageID>7dc1a6b9-5e84-4ee8-b801-816f4eccbe26</MessageID><MessageDate>.....
The method public XMLDiff(final String file1, final String file2) requires 2 file locations. Instead of a file location, I have the above stated string. What is the best way to persist this string in the form of an XML document, so that I can get its location and pass it to XMLDiff?
If the string contains XML data, then you just need to write it to disk. I would recommend a method such as FileUtils.write() from commons-io.
For example:
String xml = ...
FileUtils.write(new File("path/to/output"), xml, "UTF-8");
Of course, replace "UTF-8" with whatever encoding is advertised in your XML header.
If you are using Java 7, you don't need 3rd party libs and still only need very short code via use of the new IO api:
Path path = Files.createTempFile("prefix", null);
Files.write(path, yourXMLString.getBytes(Charset.forName("UTF-8")) , StandardOpenOption.CREATE, StandardOpenOption.WRITE);
String pathToTheFile = path.toString();
With this you can simply create two temporary files, fill them with the XML string and then provide the path strings to your library function.

Read Xml Data and store in Text file Dynamically

I need to read XMl Data and store it in Text File, In the above code i am hard Coding getTagValue for all the Tag Names, If they are 4 tag names i can hardcode getTagValuebut now i had 200 tags and how can i read data into text file without hard coding getTagValue
When using DOM to parse the XML you must know the exact structure of the XML, so ther is no real way to avoid what your are doing.
If you have an XSD (if not you can write one), you can generate a Java object from it using some Xml binding framework like XmlBeans and then with one line you can parse the XML and start working with regular java object.
A sample code would be:
File xmlFile = new File("c:\employees.xml");
// Bind the instance to the generated XMLBeans types.
EmployeesDocument empDoc =
EmployeesDocument.Factory.parse(xmlFile);
// Get and print pieces of the XML instance.
Employees emps = empDoc.getEmployees();
Employee[] empArray = emps.getEmployeeArray();
for (int i = 0; i < empArray.length; i++)
{
System.out.println(empArray[i]);
}

How to determine whether a given string is an .xml file

I have an issue that I get some some response as a String.
This String could be a normal string,number etc.. or an .xml file.
Now ,when I get an xml file, I want to treat it differently.
I am not able to distinguish between a string or an .xml file.
Also, this xml file could have some syntatic error.
Please suggest , how do I go ahead
Code is like this:
Document document = reader.read(new StringReader(xml));
where xml can be a string or an xml file itself.
If xml is a string , it is fine but if it is an xml file and with some syntax error then it should throw exception
If it is a proper XML document it should begin with a XML declaration. If that's there, it's intended to be a conforming XML document. If that's not there it cannot be a conforming XML document.
If you are using a coding language like C#, then you can use - XmlDocument.loadxml -
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx
This will throw error if the string is not in correct xml format.

Categories

Resources