Java 由逗号分隔的arraylist的串联
我需要解析包含重复元素的xml数据,并连接所有以逗号分隔的arraylist 我想要o/p字符串,如john、john1、john2、john3、john4,但我正在获得输出:、john、、john1、、john2、、john3、、john4、Java 由逗号分隔的arraylist的串联,java,Java,我需要解析包含重复元素的xml数据,并连接所有以逗号分隔的arraylist 我想要o/p字符串,如john、john1、john2、john3、john4,但我正在获得输出:、john、、john1、、john2、、john3、、john4、 package eclipsepackage; import javax.xml.parsers.*; import org.xml.sax.InputSource; import org.w3c.dom.*; import java.io.*
package eclipsepackage;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;
import java.util.ArrayList;
public class xmlparserclass {
public static void main(String[] args) {
String xmlRecords =
"<data>" +
" <service_add>" +
" <service_cd>John</service_cd>" +
" <service_cd>John1</service_cd>" +
" <service_cd>John2</service_cd>" +
" <service_cd>John3</service_cd>" +
" <service_cd>John4</service_cd>" +
" </service_add>" +
"</data>";
try {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(is);
NodeList nPanList = doc.getElementsByTagName("service_add");
for(int temp = 0 ; temp <nPanList.getLength(); temp++){
Node nNode = nPanList.item(temp);
Element eElement = (Element) nNode;
NodeList childList = eElement.getChildNodes();
String [] sPANNO = new String[childList.getLength()] ;
ArrayList list = new ArrayList();
for(int i = 0; i < childList.getLength(); i++){
Node childNode = childList.item(i);
list.add(childNode.getTextContent());
}
if (list.size() >= 1) {
System.out.print(list.get(0));
}
for (int i = 1; i < list.size(); i++) {
System.out.print(", " + list.get(i));
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
package eclipsepackage;
导入javax.xml.parsers.*;
导入org.xml.sax.InputSource;
导入org.w3c.dom.*;
导入java.io.*;
导入java.util.ArrayList;
公共类xmlparserclass{
公共静态void main(字符串[]args){
字符串XML记录=
"" +
" " +
“约翰”+
“约翰1”+
“约翰2”+
“约翰3”+
“约翰4”+
" " +
"";
试一试{
DocumentBuilderFactory dbf=
DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
InputSource is=新的InputSource();
is.setCharacterStream(新的StringReader(xmlRecords));
文档doc=db.parse(is);
NodeList nPanList=doc.getElementsByTagName(“服务添加”);
对于(内部温度=0;温度=1){
系统输出打印(list.get(0));
}
对于(inti=1;i
我删除了双引号后的空格,然后它就起作用了。所有文本元素都以逗号分隔。您确定存储在ArrayList中的文本元素不包含任何逗号吗?谢谢,我意识到我的文本中有一些空格。