使用Java模式和Matcher将XML单词插入MySQL
我想使用MySQL数据库跟踪群聊中的单词使用情况。当前传递到insertWords方法的消息是XML字符串。XML字符串可以具有特殊字符,如使用Java模式和Matcher将XML单词插入MySQL,java,mysql,xml,regex,Java,Mysql,Xml,Regex,我想使用MySQL数据库跟踪群聊中的单词使用情况。当前传递到insertWords方法的消息是XML字符串。XML字符串可以具有特殊字符,如&apos和“。有没有比使用String.replace更好的方法将XML格式的字符串转换为普通消息 如果我的信息是:我很坏,但他们不是那么“好” 我如何将其转换为:我很坏,但它们不是那么“好” 我的代码将插入apos 2次和QUOTE 2次。如何修复此问题 Pattern p = Pattern.compile("[\\w']+"); PreparedS
&apos
和“
。有没有比使用String.replace更好的方法将XML格式的字符串转换为普通消息
如果我的信息是:我很坏,但他们不是那么“好”
我如何将其转换为:我很坏,但它们不是那么“好”
我的代码将插入apos 2次和QUOTE 2次。如何修复此问题
Pattern p = Pattern.compile("[\\w']+");
PreparedStatement insertWordStmt = connection.prepareStatement("INSERT INTO word (word, count) VALUES (?, 1) " +
"ON DUPLICATE KEY UPDATE count=count+1");
public void insertWords(String msg) {
msg = msg.toLowerCase();
try {
Matcher m = p.matcher(msg);
while ( m.find() ) {
String word = msg.substring(m.start(), m.end());
insertWordStmt.setString(1, word);
insertWordStmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
要添加双引号匹配,请尝试
Pattern p = Pattern.compile("[\\w'\"]+");
评论后编辑
msg = msg.toLowerCase().replace("'","'").replace(""e;","\"");
你能举例说明应该存储什么吗?NVM我找到了我的答案。我下载了org.apache.commons包并使用了StringEscapeUtils.unescapeXml(消息);谢谢。我刚刚意识到传递到insertWords的消息是XML格式的。我编辑了我的第一篇文章。请阅读。我仍然需要帮助。NVM我找到了我的答案。我下载了org.apache.commons包并使用了StringEscapeUtils.unescapeXml(消息);