如何在Java中读取XML响应CSV数据

如何在Java中读取XML响应CSV数据,java,Java,我有如下数据: <?xml version="1.0"?> <response> "product_code","description","brand_Code","price","start_date","end_date","earliest_shipping_date","status"

我有如下数据:

<?xml version="1.0"?>
<response>
"product_code","description","brand_Code","price","start_date","end_date","earliest_shipping_date","status","available_indicator"
"139005L","Cheryl's Crunchy Tree Cookies - Caseof 48","1001","74.9100","2013-12-03","2021-12-31","2013-12-05","A","Y"
"139057","Cheryl's Get Well Cookie Cards - Case of 25","1001","40.6300","2014-01-10","2021-12-31","2014-01-20","A","N"
</response>

“产品代码”、“说明”、“品牌代码”、“价格”、“开始日期”、“结束日期”、“最早发货日期”、“状态”、“可用指示器”
“139005L”、“谢丽尔松脆树曲奇-Caseof 48”、“1001”、“74.9100”、“2013-12-03”、“2021-12-31”、“2013-12-05”、“A”、“Y”
“139057”、“Cheryl's Get Well Cookie卡-一盒25”、“1001”、“40.6300”、“2014-01-10”、“2021-12-31”、“2014-01-20”、“A”、“N”
我需要读取此数据并将其作为行插入表中。

如何在Java中读取此类数据?

您需要解析XML并提取标记的文本内容。该文本内容本质上是文件的内容。根据您问题中的示例XML,我假设您希望跳过第一行,并将剩余的每一行拆分为单独的值。下面的代码演示了如何执行此操作。注意,我将您的问题中的XML数据复制到一个名为response.XML的文件中

导入java.io.File;
导入java.io.IOException;
导入java.util.array;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入org.w3c.dom.Document;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.xml.sax.SAXException;
公共类XmlParse{
公共静态void main(字符串[]args){
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
试一试{
DocumentBuilder db=dbf.newDocumentBuilder();
documentdoc=db.parse(新文件(“response.xml”);
NodeList responses=doc.getElementsByTagName(“响应”);
int count=responses.getLength();
字符串动词=count==1?“是”:“是”;
字符串复数=count==1?”:“s”;
System.out.printf(“有%s%d个响应%s.%n”,动词,计数,复数);
节点响应=响应。项(0);
String content=response.getTextContent();
字符串[]行=content.split(“\n”);
计数=行数。长度;
动词=count==1?“是”:“是”;
复数=count==1?“:“s”;
System.out.printf(“有%s%d行%s.%n”,动词,计数,复数);
对于(int i=2;i<4;i++){
字符串[]列=行[i]。拆分(“,”;
System.out.println(Arrays.toString(columns));
}
}
捕获(IOException | ParserConfiguration异常| SAXException x){
x、 printStackTrace();
}
}
}
这是我运行上述代码时得到的输出

有1个响应。
有四行。
[“139005L”、“Cheryl's Crunchy Tree Cookies-Caseof 48”、“1001”、“74.9100”、“2013-12-03”、“2021-12-31”、“2013-12-05”、“A”、“Y”]
[“139057”、“Cheryl's Get Well Cookie卡-一盒25”、“1001”、“40.6300”、“2014-01-10”、“2021-12-31”、“2014-01-20”、“A”、“N”]
请注意,我不想猜你所说的

作为行插入到表中

你是说数据库表吗


有许多在线教程解释如何使用java代码解析XML,其中包括Oracle java教程中的一条线索。

我强烈推荐类似于而不是使用您自己的CSV解析器的内容。例如,这一个不处理转义引号,它工作正常。谢谢你的帮助。欢迎使用堆栈溢出。请学习如何使用堆栈溢出,并阅读如何提高问题的质量。然后检查,看看你可以问什么问题。请显示您已尝试过的尝试以及尝试后收到的问题/错误消息。