Member Avatar for mehnihma

I am trying to build XML to csv conversion
The problem is extended attributes ( <AttrList> ) that I do not know how to read:

This is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<ProductCatalog>
  <Product>
    <ProductCode>FFC4UINTSATA</ProductCode>
    <Vendor>INTEL</Vendor>
    <ProductType>Cable</ProductType>
    <ProductCategory>Cables</ProductCategory>
    <ProductDescription>INTEL SAS/SATA internal cable (4xSerial ATA 7-pin (Male) - 1x (Male))</ProductDescription>
    <Image>https://www.it4profit.com/catalogimg/wic/1/FFC4UINTSATA</Image>
    <ProductCard>https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=91013140652098904&amp;THEME=asbis&amp;LANG=hr</ProductCard>
    <AttrList>
      <element Name="Cable Functionality" Value="SAS/SATA internal cable"/>
      <element Name="Platform Compability" Value="PC"/>
      <element Name="Left Connector Type" Value="Serial ATA 7-pin"/>
      <element Name="Left Connector Gender" Value="Male"/>
      <element Name="Left Connector Quantity" Value="4"/>
      <element Name="Right Connector Gender" Value="Male"/>
      <element Name="Right Connector Quantity" Value="1"/>
      <element Name="Package Type" Value="Retail"/>
      <element Name="Warranty Products returnable" Value="Yes"/>
      <element Name="Warranty Term (month)" Value="36 month"/>
      <element Name="Warranty validation Criteria" Value="Serial Number"/>
      <element Name="Pack Weight Brutto (kg)" Value="1.5 kg"/>
      <element Name="Pieces in pack" Value="1"/>
    </AttrList>
    <MarketingInfo>
      <element>Intel is the world leader in the field of computer engineering, computer innovations, development of software and hardware. Specialists Intel did not stop searching for new ways to make the next leap ahead - in the IT-technology, education, culture, production and social sphere.</element>
    </MarketingInfo>
    <Images>
      <Image>https://content.it4profit.com/pimg/s/resize/160x160x160x160/91021125613081351.jpg</Image>
      <Image>https://content.it4profit.com/pimg/s/resize/400x300x400x300/91021125548056576.jpg</Image>
      <Image>https://content.it4profit.com/pimg/s/resize/200x150x200x150/91021125605627770.jpg</Image>
      <Image>https://content.it4profit.com/pimg/s/resize/75x56x75x56/91021125621051836.jpg</Image>
      <Image>https://content.it4profit.com/pimg/s/resize/600x600x600x600/91021125540313491.jpg</Image>
      <Image>https://content.it4profit.com/pimg/s/resize/260x195x260x195/91021125557081037.jpg</Image>
    </Images>
  </Product>
  </ProductCatalog>

This is that I did so far:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class NewClass {

public static void main(String args[]) {
try {

File stocks = new File("Stocks.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(stocks);
doc.getDocumentElement().normalize();

System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("Product");
System.out.println("==========================");

for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("ProductCode: " + getValue("ProductCode", element));
System.out.println("Vendor: " + getValue("Vendor", element));
System.out.println("ProductType: " + getValue("ProductType", element));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
}

If there is an easier way or is someone knows program that can read this xml and convert it to csv I would be very happy :D
Thanks

Recommended Answers

All 2 Replies

Can you post what the csv file would look like given the XML you posted for input?

Member Avatar for mehnihma

Something like

ProductCode,Vendor,ProductType,ProductCategory,ProductDescription,Image,Images,Weight,MarketingInfo
FFC4UINTSATA,INTEL,Cable,Cable,INTEL SAS/SATA internal cable (4xSerial ATA 7-pin (Male) - 1x (Male)),https://www.it4profit.com/catalogimg/wic/1/FFC4UINTSATA,https://content.it4profit.com/pimg/s/resize/260x195x260x195/91021125557081037.jpg,1.5,Intel is the world leader in the field of computer engineering, computer innovations, development of software and hardware. Specialists Intel did not stop searching for new ways to make the next leap ahead - in the IT-technology, education, culture, production and social sphere.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.