0

I want to replace colon (:) in all tag values. However my xml has namespace suffix. How can I replace it safely by using 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>

I want result like this:

<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>
Roger Chan
  • 1,293
  • 2
  • 11
  • 24
  • 5
    Parse the xml and replace `:` with empty string. – Darshan Lila Oct 07 '14 at 06:58
  • 2
    Using an XML API is the key here - don't just treat this as a big string. – Jon Skeet Oct 07 '14 at 07:02
  • [Obligatory link](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). Use SAX or StAX - they will walk the document for you, all you need to do is replace. – Boris the Spider Oct 07 '14 at 07:08
  • 1
    Use an XML parser as others noted. A _quick dirty_ solution to replace colons with equal signs in values: `String xml = "your xml here".replace("ns:","ns;").replace(':','=').replace("ns;", "ns:");` – icza Oct 07 '14 at 07:27
  • But problem is the suffix of namespace may be ns1: ns2:, it is hard to use the dirty solution. Any solution can replace it via regular expression? – Roger Chan Oct 07 '14 at 07:52

1 Answers1

0

Have you tried a replace of the specific tags like below?

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;
        }
    } 
}
Kokkie
  • 546
  • 6
  • 15