我有以下xml
getReadableDatabase().rawQuery(
String.format(
"SELECT %s, %s FROM %s",
DreamContract.Dream._ID,
DreamContract.Dream.COLUMN_NAME_DREAM_TITLE,
DreamContract.Dream.DREAM_TABLE_NAME
),
null
);
从上面的xml中,我想要替换单词“Hello”的出现,它是标记内容的一部分,但不是标记属性的一部分。
我想要以下输出(HI替换Hello):
<some tag>
<some_nested_tag attr="Hello"> Text </some_nested_tag>
Hello world Hello Programming
</some tag>
我尝试了java正则表达式以及一些DOM解析器教程,但没有任何运气。我在这里发帖求助,因为我在我的项目中有限时间可以解决这个问题。帮助将不胜感激。
答案 0 :(得分:2)
这可以通过使用负面的后视来完成。
试试这个正则表达式:
(?<!attr=")Hello
它将匹配 attr = 之前没有 Hello 。
所以你可以试试这个:
str = str.replaceAll("(?<!attr=")Hello", "Hi");
也可以通过否定前瞻来完成:
Hello(?!([^<]+)?>)
答案 1 :(得分:0)
string.replaceAll("(?i)\\shello\\s", " HI ");
正则表达式说明:
\sHello\s
Options: Case insensitive
Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s»
Match the character string “Hello” literally (case insensitive) «Hello»
Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s»
hi
Insert the character string “ HI ” literally « HI »
答案 2 :(得分:0)
XSLT是一种将XML文档转换为其他XML文档的语言。您可以匹配包含&#39; Hello&#39;的所有文本节点。并替换那些特定节点的内容。
在Java中使用XSLT的一个小例子:
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
public class TestMain {
public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("transform.xslt"));
Transformer transformer = factory.newTransformer(xslt);
Source text = new StreamSource(new File("input.xml"));
transformer.transform(text, new StreamResult(new File("output.xml")));
}
}
使用XSLT替换字符串有一个很好的问题 - 你可以在那里找到一个XSLT模板的例子: XSLT string replace
答案 3 :(得分:0)
这是一个使用SAX解析器的完整功能示例。它适用于您的情况,只需对this example
进行最少的更改实际替换发生在MyCopyHandler#endElement()和MyCopyHandler#startElement()中,XML元素文本内容收集在MyCopyHandler#characters()中。请注意缓冲区维护 - 在处理混合元素内容(文本和子元素)时非常重要
我知道XSLT解决方案也是可行的,但它不是那么便携。
public class XMLReplace {
/**
* @param args
* @throws SAXException
* @throws ParserConfigurationException
*/
public static void main(String[] args) throws Exception {
final String str = "<root> Hello <nested attr='Hello'> Text </nested> Hello world Hello Programming </root>";
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(new MyErrorHandler());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter out = new PrintWriter(baos);
MyCopyHandler duper = new MyCopyHandler(out);
reader.setContentHandler(duper);
InputSource is = new InputSource(new StringReader(str));
reader.parse(is);
out.close();
System.out.println(baos);
}
}
class MyCopyHandler implements ContentHandler {
private boolean namespaceBegin = false;
private String currentNamespace;
private String currentNamespaceUri;
private Locator locator;
private final PrintWriter out;
private final StringBuilder buffer = new StringBuilder();
public MyCopyHandler(PrintWriter out) {
this.out = out;
}
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
public void startDocument() {
}
public void endDocument() {
}
public void startPrefixMapping(String prefix, String uri) {
namespaceBegin = true;
currentNamespace = prefix;
currentNamespaceUri = uri;
}
public void endPrefixMapping(String prefix) {
}
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
// Flush buffer - needed in case of mixed content (text + elements)
out.print(buffer.toString().replaceAll("Hello", "HI"));
// Prepare to collect element text content
this.buffer.setLength(0);
out.print("<" + qName);
if (namespaceBegin) {
out.print(" xmlns:" + currentNamespace + "=\"" + currentNamespaceUri + "\"");
namespaceBegin = false;
}
for (int i = 0; i < atts.getLength(); i++) {
out.print(" " + atts.getQName(i) + "=\"" + atts.getValue(i) + "\"");
}
out.print(">");
}
public void endElement(String namespaceURI, String localName, String qName) {
// Process text content
out.print(buffer.toString().replaceAll("Hello", "HI"));
out.print("</" + qName + ">");
// Reset buffer
buffer.setLength(0);
}
public void characters(char[] ch, int start, int length) {
// Store chunk of text - parser is allowed to provide text content in chunks for performance reasons
buffer.append(Arrays.copyOfRange(ch, start, start + length));
}
public void ignorableWhitespace(char[] ch, int start, int length) {
for (int i = start; i < start + length; i++)
out.print(ch[i]);
}
public void processingInstruction(String target, String data) {
out.print("<?" + target + " " + data + "?>");
}
public void skippedEntity(String name) {
out.print("&" + name + ";");
}
}
class MyErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
show("Warning", e);
throw (e);
}
public void error(SAXParseException e) throws SAXException {
show("Error", e);
throw (e);
}
public void fatalError(SAXParseException e) throws SAXException {
show("Fatal Error", e);
throw (e);
}
private void show(String type, SAXParseException e) {
System.out.println(type + ": " + e.getMessage());
System.out.println("Line " + e.getLineNumber() + " Column " + e.getColumnNumber());
System.out.println("System ID: " + e.getSystemId());
}
}