Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting required attributes to output using Transformer Class

 
Prashanth Chandra
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a XML file which I read and convert that to a CSV file using the Transformer class. It wirtes the data exaclty but I have to get only specific values from XML based on the inputs.

<Product ModelNumber="A3316ABSBB">
<DataSource Name="SPP">
<Column Name="CARTON_LENGTH">31.3</Column>
<Column Name="PROD_DEPT_NAME">REFRIGERATION</Column>
<Column Name="SUPERSEDED_DATE">1900-01-01</Column>
<Column Name="NET_WEIGHT">144</Column>
<Column Name="SKU_DESCRIPTION">16 cu ft, wire, recessed handl</Column>
<Column Name="WHITE_GOOD">Y</Column>
</Product>
<Product ModelNumber="A3316ABSWW">
<DataSource Name="SPP">
<Column Name="CARTON_LENGTH">31.3</Column>
<Column Name="PROD_DEPT_NAME">REFRIGERATION</Column>
<Column Name="SUPERSEDED_DATE">1900-01-01</Column>
<Column Name="NET_WEIGHT">144</Column>
<Column Name="SKU_DESCRIPTION">16 cu ft, wire, recessed handl</Column>
<Column Name="WHITE_GOOD">Y</Column>
</Product>

Above is my XML file. In this way I will have thousands of attributes, I will have to retireve the data only for specific modelnumbers.

Below is my code which wrks fine and retireves all the models into a CSV file:

TransformerFactory factory = TransformerFactory.newInstance();
StreamSource streamSource = new StreamSource(styleSheetName);
Transformer transformer = factory.newTransformer(streamSource);
XMLReader reader = XMLReaderFactory.createXMLReader();
//sourceXmlReader and xsltResultWriter is passed as an inputstream and outputstream
InputSource sourceReader = new InputSource(sourceXmlReader);
Source source = new SAXSource(reader, sourceReader);
Result result = new StreamResult(xsltResultWriter);
for (String parameterName : this.getParamaters().keySet())
{
transformer.setParameter(parameterName, this.getParamaters().get(parameterName));
}
transformer.transform(source, result);
sourceXmlReader.close();
xsltResultWriter.close();

regards
Prashanth
 
Paul Clapham
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have an XML document. And you have some code which works fine. But do you have a question? If so, what is it?
 
Prashanth Chandra
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably you will have to read everything what I have written, you might understand my question
 
Paul Clapham
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. I read it again and I still don't understand what you want to know. Normally when somebody doesn't understand, it's your responsibility to fix that problem by providing more information. I suggest you try that in this case if you are interested in getting an answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic