Recently I was using Apache Digester to convert( Transform) a XML content into List of Objects.
Out of all the APIs I tried, I found digester was the best solution for my of requirement.
For people who didnot use Digester before, I would recommend giving it a try. It will make your life
easier. You can go home from work early and spend some quality time with quality people.
I will probably post some code snippets on how to use Digester, but this post is not about that.
Coming back to point.
Just like any other XML, the XML content I was trying to parse, contained few attributes, alongwith the elements.
Now digester might be having some features to take care of attributes, but I found it hard to follow.
I created the following XSLT instead. Which, when applied to a XML, transforms all it's attributes into TAGS.
The XSLT is generic enough to be used on any xml content.
If you are not sure, how to apply this xslt to your XML, keep reading. I have some sample code too.
Firstly, the XSLT content:
It is a small XSLT and should be easy to understand. In case you have problem understanding the logic, ask me.
Sample code to transform any XML using any XSLT:
Prerequisite:-
A JAVA environment offcourse. Lets not get into that deep. Amongst all the libraries that come with the distribution, the one you actively need is
Here is some sample code. The method names are self-explanatory.
I hope this helps guys. Let me know if there are any issues.
Out of all the APIs I tried, I found digester was the best solution for my of requirement.
For people who didnot use Digester before, I would recommend giving it a try. It will make your life
easier. You can go home from work early and spend some quality time with quality people.
I will probably post some code snippets on how to use Digester, but this post is not about that.
Coming back to point.
Just like any other XML, the XML content I was trying to parse, contained few attributes, alongwith the elements.
Now digester might be having some features to take care of attributes, but I found it hard to follow.
I created the following XSLT instead. Which, when applied to a XML, transforms all it's attributes into TAGS.
The XSLT is generic enough to be used on any xml content.
If you are not sure, how to apply this xslt to your XML, keep reading. I have some sample code too.
Firstly, the XSLT content:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*" />
<xsl:template match="*">
<xsl:copy >
<xsl:if test="@*">
<xsl:for-each select="@*">
<xsl:element name="{name()}" >
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:if>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
It is a small XSLT and should be easy to understand. In case you have problem understanding the logic, ask me.
Sample code to transform any XML using any XSLT:
Prerequisite:-
A JAVA environment offcourse. Lets not get into that deep. Amongst all the libraries that come with the distribution, the one you actively need is
rt.jar
Here is some sample code. The method names are self-explanatory.
public static String convertAllAttributesToElements(String xmlContent) throws TransformerException{
javax.xml.transform.Source xmlSource =new javax.xml.transform.stream.StreamSource(new StringReader(xmlContent));
javax.xml.transform.Source xsltSource =new javax.xml.transform.stream.StreamSource(xsltFile);
return transformContent(xmlSource,xsltSource);
}
public static String convertAllAttributesToElements(File xmlFile) throws TransformerException{
javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(xmlFile);
javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
return transformContent(xmlSource,xsltSource);
}
private static String transformContent(javax.xml.transform.Source xmlSource,javax.xml.transform.Source xsltSource) throws TransformerException{
String modifiedContent = null;
javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
StreamResult result = new javax.xml.transform.stream.StreamResult(outStream);
javax.xml.transform.Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, result);
byte[] b = ((ByteArrayOutputStream)result.getOutputStream()).toByteArray();
modifiedContent = new String (b);
//removing any line feed or spaces to save bytes.
modifiedContent = modifiedContent.replaceAll("[\\n\\r\\s]+","");
return modifiedContent;
}
I hope this helps guys. Let me know if there are any issues.
1 comment:
hi, can you help me to convert elements to attributes
regards
Post a Comment