我正在尝试从Java属性文件创建XML文件。其中标签名称是属性文件的点分隔键部分,标签值是属性文件的值部分。
这是我的属性文件:
Inter.con.Compa.Plugin.Name=Compacon
Inter.con.Compa.Plugin.Config.Mode=Fast
Inter.con.Compa.Plugin.Config.DataSrc=Compa
Inter.con.Compa.Plugin.Config.Query=DONT KNOW
Inter.con.Compa.Plugin.Config.ClassName=FastCompaconComponentImpl
Inter.con.Compa.Plugin.Name=CompaconComponent
Inter.con.Compa.Plugin.Config.Mode=Detailed
Inter.con.Compa.Plugin.Config.DataSrc=Compa
Inter.con.Compa.Plugin.Config.Query=DONT KNOW
Inter.con.Compa.Plugin.Config.ClassName=DetailedCompaconComponentImpl
Inter.con.port.Plugin.Name=CompaconComponent
Inter.con.port.Plugin.Config.Mode=Fast
Inter.con.port.Plugin.Config.DataSrc=Compa
Inter.con.port.Plugin.Config.Query=DONT KNOW
Inter.con.port.Plugin.Config.ClassName=FastCompaconComponentImpl
Inter.con.port.Plugin.Name=CompaconComponent
Inter.con.port.Plugin.Config.Mode=Detailed
Inter.con.port.Plugin.Config.DataSrc=Compa
Inter.con.port.Plugin.Config.Query=DONT KNOW
Inter.con.port.Plugin.Config.ClassName=DetailedCompaconComponentImpl
这是我写给xml文件的程序。
public static void myConvertToXML(String output_filename)
throws IOException {
Element root = new Element("Property");
root.setAttribute("schemaVersion", "1.0");
Namespace xmlns = Namespace.getNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
root.setNamespace(xmlns);
Document doc = new Document(root);
BufferedReader buffer = new BufferedReader(
new FileReader(inputFilePath));
String line;
while ((line = buffer.readLine()) != null) {
line = line.trim();
String[] inputLine = line.split("=");
String keyPart = inputLine[0].trim();
String valuePart = "";
if (inputLine.length == 1) {
valuePart = " ";
} else {
valuePart = inputLine[1].trim();
}
addToXML(root, keyPart, valuePart);
}
XMLOutputter outputter = new XMLOutputter();
FileOutputStream output = new FileOutputStream(output_filename);
outputter.output(doc, output);
System.out.println("Done");
}
private static void addToXML(Element root, String key_Part,
String value_Part) {
int dot;
String name = key_Part;
Element current = root;
Element test = null;
while ((dot = name.indexOf(".")) != -1) {
String subName = name.substring(0, dot);
name = name.substring(dot + 1);
if ((test = current.getChild(subName)) == null) {
Element subElement = new Element(subName);
current.addContent(subElement);
current = subElement;
} else {
current = test;
}
}
Element last = new Element(name);
last.setText(value_Part);
current.addContent(last);
}
我得到的输出
<?xml version="1.0" encoding="UTF-8"?>
<xsi:Source xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaVersion="1.0">
<Inter>
<con>
<Compa>
<Plugin>
<Name>Compacon</Name>
<Config>
<Mode>Fast</Mode>
<DataSrc>Compa</DataSrc>
<Query>DONT KNOW</Query>
<ClassName>FastCompaconComponentImpl</ClassName>
<Mode>Detailed</Mode>
<DataSrc>Compa</DataSrc>
<Query>DONT KNOW</Query>
<ClassName>DetailedCompaconComponentImpl</ClassName>
</Config>
<Name>CompaconComponent</Name>
</Plugin>
</Compa>
<port>
<Plugin>
<Name>CompaconComponent</Name>
<Config>
<Mode>Fast</Mode>
<DataSrc>Compa</DataSrc>
<Query>DONT KNOW</Query>
<ClassName>FastCompaconComponentImpl</ClassName>
<Mode>Detailed</Mode>
<DataSrc>Compa</DataSrc>
<Query>DONT KNOW</Query>
<ClassName>DetailedCompaconComponentImpl</ClassName>
</Config>
<Name>CompaconComponent</Name>
</Plugin>
</port>
</con>
</Inter>
</xsi:Source>
预期产出:
<?xml version="1.0" encoding="UTF-8"?>
<xsi:Source xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaVersion="1.0">
<Inter>
<con>
<Compa>
<Plugin>
<Name>Compacon</Name>
<Config>
<Mode>Fast</Mode>
<DataSrc>Compa</DataSrc>
<Query>DONT KNOW</Query>
<ClassName>FastCompaconComponentImpl</ClassName>
</Config>
</Plugin>
<Plugin>
<Name>CompaconComponent</Name>
<Config>
<Mode>Detailed</Mode>
<DataSrc>Compa</DataSrc>
<Query>DONT KNOW</Query>
<ClassName>DetailedCompaconComponentImpl</ClassName>
</Config>
</Plugin>
</Compa>
<port>
<Plugin>
<Name>CompaconComponent</Name>
<Config>
<Mode>Fast</Mode>
<DataSrc>Compa</DataSrc>
<Query>DONT KNOW</Query>
<ClassName>FastCompaconComponentImpl</ClassName>
</Config>
</Plugin>
<Plugin>
<Name>CompaconComponent</Name>
<Config>
<Mode>Detailed</Mode>
<DataSrc>Compa</DataSrc>
<Query>DONT KNOW</Query>
<ClassName>DetailedCompaconComponentImpl</ClassName>
</Config>
</Plugin>
</port>
</con>
</Inter>
</xsi:Source>
它直接在标签中添加所有内容我想要两个单独的标签。有谁可以帮忙。如果我遗漏了任何内容,请告诉我