我在xml文件中有以下部分:
<database>
<document form='Record'>
<item name='SystemsList'><text>2000;Generl;All equipment<break/>
2001;General;All equipment<break/>
2002;General;All equipment<break/>
2003;General;All Equipment</text></item>
<item name='RmNumber'><text>001</text></item>
<item name='Reason'><text>Don't know</text></item>
<item name='Something'><text>smth</text></item>
</document>
</database>
现在我使用以下代码:
Document doc1 = dBuilder.parse(fXmlFile1);
doc1.getDocumentElement().normalize();
NodeList kList1 =doc1.getElementsByTagName("item");
for(int temp=0;temp<kList1.getLength();temp++)
{
Node kNode1=kList1.item(temp);
//System.out.println("\nCurrent Element :" + kNode.getNodeName());
if (kNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) kNode1;
//System.out.println("node name"+eElement.getNodeName());
Node in=eElement.getFirstChild();
//System.out.println("__________________"+in.getFirstChild().getTextContent());
//System.out.println("IN text content----:"+in.getTextContent()+":--------");
if(eElement.getAttribute("name").equals("SystemsList")==true)
{
NodeList kList2=in.getChildNodes();
//if((in.getTextContent()!=null)&&!(in.getTextContent()).isEmpty()&& !(in.getTextContent().length()==0))
//{
for(int k=0;k<kList2.getLength();k++)
{
Node kNode2 = kList2.item(k);
if((kNode2.getTextContent()!=null)&&!(kNode2.getTextContent()).isEmpty()&& !(kNode2.getTextContent().length()==0))
stringBuilder.append(kNode2.getTextContent()+"\n");
}
//}
}
}
}
String s=new String(stringBuilder);
String sa[]=s.split("\n");
System.out.println("size"+sa.length);
for(String st:sa)
{
System.out.println(st);
}
此代码生成以下String = “2000; General; All equipment2001; General; All equipment2002; General; All equipment2003; General; All Equipment”。
问题是我如何将这个带有break的xml部分变为ArrayList,其中每个元素都是上面xml的1行,或者只是为了生成一个String数组,f / e:SystemsListByYear [0] =“2000 ; Generl;所有设备“,SystemsListByYear [1] =”2001; Generl;所有设备“等。
P.S。我使用DOM库。
编辑-问题到正确 编辑部分:
if (kNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) kNode1;
//System.out.println("node name"+eElement.getNodeName());
Node in=eElement.getFirstChild();
//System.out.println("__________________"+in.getFirstChild().getTextContent());
//System.out.println("IN text content----:"+in.getTextContent()+":--------");
if(eElement.getAttribute("name").equals("SystemsList")==true)
{
NodeList kList2=in.getChildNodes();
//if((in.getTextContent()!=null)&&!(in.getTextContent()).isEmpty()&& !(in.getTextContent().length()==0))
//{
for(int k=0;k<kList2.getLength();k++)
{
Node kNode2 = kList2.item(k);
if((kNode2.getTextContent()!=null)&&!(kNode2.getTextContent()).isEmpty()&& !(kNode2.getTextContent().length()==0))
stringBuilder.append(kNode2.getTextContent()+"\n");
}
//}
}
}
答案 0 :(得分:1)
然后这将解决您的问题
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
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 Test {
public static void main(String args[]) throws Exception {
FileInputStream fileInputStream = new FileInputStream(new File(
"src/file.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc1 = builder.parse(fileInputStream);
doc1.getDocumentElement().normalize();
NodeList kList1 = doc1.getElementsByTagName("item");
List<String> alist=new ArrayList<String>();
StringBuilder stringBuilder=new StringBuilder();
String SystemsListByYear;
for (int temp = 0; temp < kList1.getLength(); temp++) {
Node kNode1 = kList1.item(temp);
System.out.println("\nCurrent Element :" + kNode1.getNodeName());
if (kNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) kNode1;
System.out.println("node name"+eElement.getNodeName());
Node in=eElement.getFirstChild();
if((in.getTextContent()!=null)&&!(in.getTextContent()).isEmpty()&& !(in.getTextContent().length()==0))
stringBuilder.append(in.getTextContent());
}
}
String s=new String(stringBuilder);
String sa[]=s.split("\n");
System.out.println("size"+sa.length);
for(String st:sa)
{
System.out.println(st);
}
}
}
<强>输出强>
node nameitem
size4
2000;Generl;All equipment
2001;General;All equipment
2002;General;All equipment
2003;General;All Equipment
答案 1 :(得分:0)
将文字内容拆分为<break/>
,并将每个拆分元素添加到ArrayList
。