好吧所以我是编码的新手,并且一直试图让这个java代码做我想做的事情,但到目前为止,除了试图让它正常工作的问题之外什么都没有。
从我可以告诉它到达代码的这一部分:
if (str.contains("<TABLE")) {
tableName = str.split("\'")[1];
}
并且代码中断了这个错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at decompose.FileDecompose.main(FileDecompose.java:30)
我尝试将[1]更改为[0],我收到此错误:
java.io.FileNotFoundException: e:\Games\libconfig_twn\decompose\ <TABLE name='KeyInfo' RowCount='77' TableInfoID = '17' FieldCnt = '4'>.xml (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at decompose.FileDecompose.main(FileDecompose.java:33)
我正在使用java 8 up 161
package decompose;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileDecompose {
public static void main(String[] args) {
try {
StringBuffer sb = new StringBuffer("");
// here goes decrypted libconfig file
FileReader reader = new FileReader("I:\\libconfig\\merge\\libconfig.xml");
BufferedReader br = new BufferedReader(reader);
String str = null;
int count = 0;
String tableName = "";
while ((str = br.readLine()) != null) {
if (!str.equals("<?xml version=\"1.0\" encoding=\"utf-8\"?>") && !str.contains("<INI TableCount")) {
sb.append(str + "\n");
if (str.contains("<TABLE")) {
tableName = str.split("\'")[1];
}
if (str.contains("</TABLE>")) {
FileWriter writer = new FileWriter(
// here goes decomposed files folder
"I:\\libconfig\\decompose\\"+ tableName + ".xml");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(sb.toString());
bw.close();
writer.close();
sb = new StringBuffer("");
}
}
}
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我一直试图拆分我的.xml文档,但到目前为止,无论我对它做了哪些修改,上面的脚本都无法正常工作,所以我要求一些帮助。
这里有一些关于我的xml文档libconfig.xml 110Mb的信息,这里是xml文档的一个例子。
<?xml version="1.0" encoding="utf-8"?>
<INI TableCount = '7786' TableInfoCount = '1262'>
<TABLE name='KeyInfo' RowCount='77' TableInfoID = '17' FieldCnt = '4'>
<FIELDINFO Name='Name' IsKey='1' DataType='String'></FIELDINFO>
<FIELDINFO Name='EnumName' IsKey='0' DataType='Enum'></FIELDINFO>
<FIELDINFO Name='InputKind' IsKey='0' DataType='Enum'></FIELDINFO>
<FIELDINFO Name='Modifyer' IsKey='0' DataType='Enum'></FIELDINFO>
<ROW>
<Name><![CDATA[Action]]></Name>
<EnumName>1</EnumName>
<InputKind>216</InputKind>
<Modifyer>0</Modifyer>
</ROW>
</TABLE>
<TABLE name='CharacterInfo' RowCount='2402' TableInfoID = '18' FieldCnt = '16'>
<FIELDINFO Name='ID' IsKey='1' DataType='Enum'></FIELDINFO>
<FIELDINFO Name='CommonName' IsKey='0' DataType='String'></FIELDINFO>
<FIELDINFO Name='CommonName_Eng' IsKey='0' DataType='String'></FIELDINFO>
<FIELDINFO Name='FileName' IsKey='0' DataType='String'></FIELDINFO>
<FIELDINFO Name='PartName' IsKey='0' DataType='String'></FIELDINFO>
<FIELDINFO Name='IllustName' IsKey='0' DataType='String'></FIELDINFO>
<FIELDINFO Name='WalkSpeed' IsKey='0' DataType='float'></FIELDINFO>
<FIELDINFO Name='SizeOnStop' IsKey='0' DataType='Enum'></FIELDINFO>
<FIELDINFO Name='SizeOnMove' IsKey='0' DataType='Enum'></FIELDINFO>
<FIELDINFO Name='RunSpeed' IsKey='0' DataType='float'></FIELDINFO>
<FIELDINFO Name='MinAtkRange' IsKey='0' DataType='INT'></FIELDINFO>
<FIELDINFO Name='MaxAtkRange' IsKey='0' DataType='INT'></FIELDINFO>
<FIELDINFO Name='ChaseRange' IsKey='0' DataType='INT'></FIELDINFO>
<FIELDINFO Name='PivotFileName' IsKey='0' DataType='String'></FIELDINFO>
<FIELDINFO Name='TransRate' IsKey='0' DataType='INT'></FIELDINFO>
<FIELDINFO Name='HideShadow' IsKey='0' DataType='bool'></FIELDINFO>
<ROW>
<ID>1</ID>
<CommonName><![CDATA[Rabbit]]></CommonName>
<CommonName_Eng><![CDATA[Rabbit]]></CommonName_Eng>
<FileName><![CDATA[data\character\chr001.nri]]></FileName>
<PartName><![CDATA[data\part\chr001_]]></PartName>
<IllustName><![CDATA[ ]]></IllustName>
<WalkSpeed>0.000000</WalkSpeed>
<SizeOnStop>2</SizeOnStop>
<SizeOnMove>0</SizeOnMove>
<RunSpeed>0.000000</RunSpeed>
<MinAtkRange>50</MinAtkRange>
<MaxAtkRange>70</MaxAtkRange>
<ChaseRange>300</ChaseRange>
<PivotFileName><![CDATA[data\character\chr001.pvt]]></PivotFileName>
<TransRate>0</TransRate>
<HideShadow>0</HideShadow>
</ROW>
</TABLE>
</INI>
好吧所以我想从<TABLE to </TABLE>
分割我的xml,然后搜索name ='???'在<TABLE name='KeyInfo' RowCount='77' TableInfoID = '17' FieldCnt = '4'>
中创建一个文件示例:
00001_KeyInfo.xml
00002_CharacterInfo.xml
依旧......
大约有11,000个表需要拆分,每个表都有不同的内容,需要有utf-8字符编码所以是的,这对我来说很难对编码很新,我希望有人可以提供帮助。 / p>
答案 0 :(得分:0)
您可以像这样使用Java xml api
public static void main(String... args) throws Exception {
String file = "/home/william/Documents/test.xml";
FileInputStream fis = new FileInputStream(new File(file));
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(fis);
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//TABLE";
NodeList nodeList = (NodeList) xpath.compile(expression).evaluate(xml, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node table = nodeList.item(i);
String name = table.getAttributes().getNamedItem("name").getTextContent();
String content = toContent(table);
save(content, name);
}
}
private static String toContent(Node node) {
// your implementation here
return null;
}
private static void save(String content, String name) {
// TODO implement
}
答案 1 :(得分:0)
我不确切地知道它为什么会失败,但这是解决这项任务的可怕方法。永远不要在XML上使用字符串匹配,始终使用正确的XML解析器。
在XSLT 2.0中,这项工作非常简单:
<xsl:template match="TABLE">
<xsl:result-document href="{@name}.xml">
<xsl:copy-of select="."/>
</xsl:result-document>
</xsl:template>