Properties class has very nice methods storeToXml and loadFromXml. But store adds header, so xml lo开发者_StackOverflow中文版oks like this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="key">value</entry>
</properties>
I don't want to have this header, because i store it in database to make it xpath serchable. And also i want to load in to Properties object. Have anyone good ideas? Thanks
Neither XPath nor Properties.loadFromXml()
care for the header. So this should work as it is.
If it's really a problem, then write the result to a StringWriter
or ByteArrayOutputStream
and remove anything before <properties>
. But that might actually cause errors loading the XML since the doctype is now missing.
Try this way:
Properties p = new Properties();
p.put("A.a", "BB");
p.put("A.b", "BB");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
p.storeToXML(bout, "Commnet!");
String sp = new String(bout.toByteArray());
sp = sp.substring(sp.indexOf('\n', sp.indexOf('\n') + 1) + 1);
System.out.println(sp);
It will give you following result:
<properties>
<comment>COmmnet!</comment>
<entry key="A.b">BB</entry>
<entry key="A.a">BB</entry>
</properties>
Just remove the header :
BufferedReader br = new BufferedReader(new FileReader(new File("filename")));
BufferedWriter wr = new BufferedWriter(new FileWriter(new File("filename_out")));
String line;
int counter = 0;
while((line=br.readLine())!= null){
if(counter > 0){
wr.write(line.trim());
}
counter++;
}
wr.close();
br.close();
You could transform this xml using a xslt that removes the doctype declaration and so store the output in database.
public class XMLTransform {
public static void main(String args[]) {
try {
StreamSource source = new StreamSource("your xml");
StreamSource stylesource = new StreamSource("your xslt");
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(stylesource);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StreamResult result = new StreamResult(new File("your xml output file"));
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Given that properties.dtd is so trivial, write and parse the XML yourself.
Output:
private static String toXml(Properties props) throws XMLStreamException {
Writer buffer = new StringWriter();
XMLStreamWriter xml = XMLOutputFactory.newFactory()
.createXMLStreamWriter(buffer);
xml.writeStartElement("properties");
for (Map.Entry<Object, Object> entry : props.entrySet()) {
xml.writeStartElement("entry");
xml.writeAttribute("key", entry.getKey()
.toString());
xml.writeCharacters(entry.getValue()
.toString());
xml.writeEndElement();
}
xml.writeEndElement();
return buffer.toString();
}
Input:
private static Properties fromXml(String xml) throws XPathException {
Properties props = new Properties();
NodeList entries =
(NodeList) XPathFactory.newInstance()
.newXPath()
.evaluate("//entry", new InputSource(new StringReader(xml)),
XPathConstants.NODESET);
for (int i = 0; i < entries.getLength(); i++) {
Node entry = entries.item(i);
props.setProperty(entry.getAttributes()
.getNamedItem("key")
.getNodeValue(), entry.getTextContent());
}
return props;
}
I'm assuming you want data as the default JDBC mapping - String.
精彩评论