c:\\c.xml内容如下:
<?xml version="1.0" encoding="GBK"?>
<List>
<Row>
<name>Tom </name>
<age>11 </age>
</Row>
<Row>
<name>Jerry </name>
<age>10 </age>
</Row>
<Row>
<name>Jerry </name>
<age>10 </age>
</Row>
<Row>
<name>Pluto </name>
<age>13 </age>
</Row>
<!-- rows 。n个 -->
</List> 不知道能不能写个代码,把c:\\c.xml的内容分割成如下
<?xml version="1.0" encoding="GBK"?>
<List>
<Row>
<name>Tom </name>
<age>11 </age>
</Row>
<!-- rows 。n个 -->
</List> --------------------------------
<?xml version="1.0" encoding="GBK"?>
<List>
<Row>
<name>Jerry </name>
<age>10 </age>
</Row>
</Row>
<!-- rows 。n个 -->
</List>
--------------------------------
<?xml version="1.0" encoding="GBK"?>
<List>
<Row>
<name>Pluto </name>
<age>13 </age>
</Row>
</List>
分别放到
c_1.xml
c_2.xml
c_3.xml
...
c_i.xml
...
c_n.xml 这n个文件中,这几个文件也需要用代码生成。有n个 <row>节点就用代码新建n个row_i.xml文件 //(i=1,2,3...n-1,n),不知道可不可以做得到?
可以用框架。
<?xml version="1.0" encoding="GBK"?>
<List>
<Row>
<name>Tom </name>
<age>11 </age>
</Row>
<Row>
<name>Jerry </name>
<age>10 </age>
</Row>
<Row>
<name>Jerry </name>
<age>10 </age>
</Row>
<Row>
<name>Pluto </name>
<age>13 </age>
</Row>
<!-- rows 。n个 -->
</List> 不知道能不能写个代码,把c:\\c.xml的内容分割成如下
<?xml version="1.0" encoding="GBK"?>
<List>
<Row>
<name>Tom </name>
<age>11 </age>
</Row>
<!-- rows 。n个 -->
</List> --------------------------------
<?xml version="1.0" encoding="GBK"?>
<List>
<Row>
<name>Jerry </name>
<age>10 </age>
</Row>
</Row>
<!-- rows 。n个 -->
</List>
--------------------------------
<?xml version="1.0" encoding="GBK"?>
<List>
<Row>
<name>Pluto </name>
<age>13 </age>
</Row>
</List>
分别放到
c_1.xml
c_2.xml
c_3.xml
...
c_i.xml
...
c_n.xml 这n个文件中,这几个文件也需要用代码生成。有n个 <row>节点就用代码新建n个row_i.xml文件 //(i=1,2,3...n-1,n),不知道可不可以做得到?
可以用框架。
import java.io.FileWriter;
import java.util.Iterator;import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;public class XmlSpliter { /**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception{
int index=0;
XMLWriter writer=new XMLWriter();
SAXReader reader=new SAXReader();
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
Document doc=reader.read(new File("c:\\c.xml"));
Element root=doc.getRootElement();
Iterator iter=root.elementIterator("Row");
String fileName="";
while(iter.hasNext()){
fileName="c:\\c_"+(index+1)+".xml";
Document _document = DocumentHelper.createDocument();
Element elemRow=(Element) iter.next();
Element myElemRoot=_document.addElement(root.getName());
Element myElemRow=myElemRoot.addElement(elemRow.getName());
myElemRow.addComment("rows 。"+index+"个");
writer=new XMLWriter(new FileWriter(fileName),format);
Iterator iter2=elemRow.elementIterator();
while(iter2.hasNext()){
Element elem=(Element) iter2.next();
Element e=myElemRow.addElement(elem.getName());
e.setText(elem.getText());
}
writer.write(_document);
index++;
writer.close();
}
}
}
要加入dom4j的jar包