如何使用Java替换xml标记中的所有冒号?
我想替换所有标记值中的冒号。但是,我的xml有名称空间后缀。如何使用Java安全地替换它 e、 g 我想要这样的结果:如何使用Java替换xml标记中的所有冒号?,java,Java,我想替换所有标记值中的冒号。但是,我的xml有名称空间后缀。如何使用Java安全地替换它 e、 g 我想要这样的结果: <ns:shop>ABC Adress 1</ns:shop> <ns:person>John Lee</ns:person> <ns:shop>DEF: Adress 2</ns:shop> <ns:person>Susan Lee</ns:person> 您是否尝试
<ns:shop>ABC Adress 1</ns:shop>
<ns:person>John Lee</ns:person>
<ns:shop>DEF: Adress 2</ns:shop>
<ns:person>Susan Lee</ns:person>
您是否尝试过以下特定标签的替换
public class replacetagcontent {
static String inputFile = "C:/temp/shop.xml";
static String outputFile = "C:/temp/shop_modified.xml";
public static void main(String[] args) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
Document doc = builder.parse(inputFile);
// locate the node(s)
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new MyNamespaceContext());
NodeList shp_nodes = (NodeList)xpath.evaluate("//ns:shop", doc, XPathConstants.NODESET);
NodeList pers_nodes = (NodeList)xpath.evaluate("//ns:person", doc, XPathConstants.NODESET);
// make the change
for (int i = 0; i < shp_nodes.getLength(); i++) {
String name_value = shp_nodes.item(i).getTextContent();
shp_nodes.item(i).setTextContent(name_value.replace(":",""));
String title_value = pers_nodes.item(i).getTextContent();
pers_nodes.item(i).setTextContent(title_value.replace(":",""));
}
// save the result
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
}
private static class MyNamespaceContext implements NamespaceContext {
public String getNamespaceURI(String prefix) {
if("ns".equals(prefix)) {
return "http://www.example.org/schema";
}
return null;
}
public String getPrefix(String namespaceURI) {
return null;
}
public Iterator getPrefixes(String namespaceURI) {
return null;
}
}
}
解析xml并将:替换为空字符串。使用xml API是这里的关键-不要将其视为一个大字符串。。使用SAX或StAX-他们将为您遍历文档,您所需要做的只是替换。如其他人所述,使用XML解析器。用等号替换冒号的快速脏解决方案:String xml=your xml here.replacens:,ns;。替换“:”,“=”。替换项;,ns:;但问题是名称空间的后缀可能是ns1:ns2:,很难使用脏的解决方案。任何解决方案都可以通过正则表达式替换它?
public class replacetagcontent {
static String inputFile = "C:/temp/shop.xml";
static String outputFile = "C:/temp/shop_modified.xml";
public static void main(String[] args) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
Document doc = builder.parse(inputFile);
// locate the node(s)
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new MyNamespaceContext());
NodeList shp_nodes = (NodeList)xpath.evaluate("//ns:shop", doc, XPathConstants.NODESET);
NodeList pers_nodes = (NodeList)xpath.evaluate("//ns:person", doc, XPathConstants.NODESET);
// make the change
for (int i = 0; i < shp_nodes.getLength(); i++) {
String name_value = shp_nodes.item(i).getTextContent();
shp_nodes.item(i).setTextContent(name_value.replace(":",""));
String title_value = pers_nodes.item(i).getTextContent();
pers_nodes.item(i).setTextContent(title_value.replace(":",""));
}
// save the result
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
}
private static class MyNamespaceContext implements NamespaceContext {
public String getNamespaceURI(String prefix) {
if("ns".equals(prefix)) {
return "http://www.example.org/schema";
}
return null;
}
public String getPrefix(String namespaceURI) {
return null;
}
public Iterator getPrefixes(String namespaceURI) {
return null;
}
}
}