使用Java/Javascript将CSV转换为自定义XML
我想用Java/Javascript将CSV转换成XML 例如,我的CSV文件如下表所示:使用Java/Javascript将CSV转换为自定义XML,javascript,java,xml,csv,export-to-xml,Javascript,Java,Xml,Csv,Export To Xml,我想用Java/Javascript将CSV转换成XML 例如,我的CSV文件如下表所示: |ID | OLO| |12345 | TLC| |12345 | VPN| |67890 | TLC| 我希望有这样一个XML文件: <?xml version='1.0' encoding='UTF-8'?> <Custom name="Custom_ListaOLO"> <Attributes> <Map> <entr
|ID | OLO| |12345 | TLC| |12345 | VPN| |67890 | TLC|
我希望有这样一个XML文件:
<?xml version='1.0' encoding='UTF-8'?>
<Custom name="Custom_ListaOLO">
<Attributes>
<Map>
<entry key="12345">
<value>
<List>
<String>TLC</String>
<String>VPN</String>
</List>
</value>
</entry>
<entry key="67890">
<value>
<List>
<String>TLC</String>
</List>
</value>
</entry>
</Map>
</Attributes>
</Custom>
薄层色谱
虚拟专用网
薄层色谱
或:
薄层色谱
虚拟专用网
您可以通过MOXy实现使用JAXB封送来满足上述要求
JAXB
Java Architecture for XML Binding(JAXB)是一个软件框架,它提供了将Java类映射到XML表示的方法
JAXB封送处理
将java对象转换为xml
MOXy
使开发人员能够处理复杂的XML结构
首先读取csv文件并创建java对象,然后使用jaxb封送和moxy实现将java对象转换为xml
尝试以下解决方案
为xml文件中表示的
和
元素创建两个java pojo类(Custom
和Entry
)
Custom.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name="Custom")
public class Custom {
private String name;
private List<Entry> entry;
public String getName() {
return name;
}
@XmlAttribute
public void setName(String name) {
this.name = name;
}
public Custom() {
entry = new ArrayList<Entry>();
}
@XmlPath("Attributes/Map/entry")
public List<Entry> getEntry() {
return entry;
}
public void setEntry(List<Entry> entry) {
this.entry = entry;
}
}
inputCSV.csv
ID,OLO
12345,TLC
12345,VPN
67890,TLC
output.xml
<?xml version="1.0" encoding="UTF-8"?>
<Custom name="Custom_ListaOLO">
<Attributes>
<Map>
<entry key="12345">
<value>
<List>
<String>TLC</String>
<String>VPN</String>
</List>
</value>
</entry>
<entry key="67890">
<value>
<List>
<String>TLC</String>
</List>
</value>
</entry>
</Map>
</Attributes>
</Custom>
薄层色谱
虚拟专用网
薄层色谱
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.xmlmodel.ObjectFactory;
public class Demo {
public static void main(String[] args) throws JAXBException {
String line;
String key = null;
Custom custom = new Custom();
Entry entry = null;
int index = 0;
try {
BufferedReader br = new BufferedReader(new FileReader("inputCSV.csv")); //get csv file
while ((line = br.readLine()) != null) { //get every single line individually in csv file
if(index > 0){ //skip the column's names (first line of csv file)
String[] value = line.split(","); //collect the comma separated values (ID and OLO) into array
if(key == null || !key.equals(value[0])){ //first line of the csv file and when find the new key value, then create new entry
if(entry != null){
custom.getEntry().add(entry); //add entry object into entry list of custom
}
key = value[0]; //assign the key value to variable (String key) for identify the new entry
entry = new Entry(); //create a new entry
entry.setKey(value[0]); //assign the key value to entry
entry.getString().add(value[1]); //add string value String list of entry with new key
}else{
entry.getString().add(value[1]); //add string value String list of entry under the same key
}
}
index++;
}
custom.setName("Custom_ListaOLO"); //set value to name attribute of custom element
custom.getEntry().add(entry); //add last entry into entry list of custom
} catch (IOException e) {
e.printStackTrace();
}
//marshaling with JAXB
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[]{Custom.class, ObjectFactory.class}, null);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(custom, new File("output.xml")); //generate the output xml file
marshaller.marshal(custom, System.out);
}
}
ID,OLO
12345,TLC
12345,VPN
67890,TLC
<?xml version="1.0" encoding="UTF-8"?>
<Custom name="Custom_ListaOLO">
<Attributes>
<Map>
<entry key="12345">
<value>
<List>
<String>TLC</String>
<String>VPN</String>
</List>
</value>
</entry>
<entry key="67890">
<value>
<List>
<String>TLC</String>
</List>
</value>
</entry>
</Map>
</Attributes>
</Custom>