Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

XMLMerge support for NameSpaces

Help
2009-05-12
2013-05-02
  • Jorge Pinto
    Jorge Pinto
    2009-05-12

    Hi all,

    I'm trying to use XMLMerge in my application to deal with really big XML files.
    It seems de API covers all my neccesities with regular XML, but I have problems when handling XML files with NameSpaces.
    I'm not sure if there's a way to make something like this:

    -- ORIGINAL --
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/CIM-everis#">
        <cim:RegularTimePoint rdf:ID="0"/>
        <cim:RegularTimePoint rdf:ID="1"/>
    </rdf:RDF>

    -- PATCH --
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/CIM-everis#">
        <cim:RegularTimePoint rdf:ID="1">SomeValue</cim:RegularTimePoint>
    </rdf:RDF>

    -- RESULT --
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/CIM-everis#">
        <cim:RegularTimePoint rdf:ID="0"/>
        <cim:RegularTimePoint rdf:ID="1">SomeValue</cim:RegularTimePoint>
    </rdf:RDF>

    I've tried to use a custom matcher (quite similar to IdMatcher), but the result is not the expected one:

    public class RdfIdMatcher implements Matcher {
        public boolean matches(Element originalElement, Element patchElement) {
            boolean b1 = originalElement.getQualifiedName().equals(patchElement.getQualifiedName());
            boolean b2 = originalElement.getAttribute("rdf:ID") != null;
            boolean b3 = patchElement.getAttribute("rdf:ID") != null;
            boolean b4 = originalElement.getAttributeValue("rdf:ID").equals(patchElement.getAttributeValue("rdf:ID"));
            return b1 && b2 && b3 && b4;
        }
    }

    The main class have something like this:

    public class PruebaXMLMerge {

        public static void main(String[] args) throws Exception{
            InputStream file1 = new FileInputStream("C:/XMLTemp/file11.xml"); 
            InputStream file2 = new FileInputStream("C:/XMLTemp/file22.xml");
            Document doc = merge(file1, file2);
            print(doc); 
        } 
       
        private static Document merge(InputStream... files) throws Exception {   
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();   
            docBuilderFactory.setIgnoringElementContentWhitespace(true);
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Configurer config = new PropertyXPathConfigurer("xpath.path1=/rdf:RDF/cim:RegularTimePoint \n matcher.path1=com.everis.denise.prueba.RdfIdMatcher");
            XmlMerge xmlM = new ConfigurableXmlMerge(config);
           
            Document doc = docBuilder.parse(xmlM.merge(files));
            return doc;
        } 
       
        private static void print(Document doc) throws Exception {   
            TransformerFactory transformerFactory = TransformerFactory.newInstance();   
            Transformer transformer = transformerFactory.newTransformer();   
            DOMSource source = new DOMSource(doc);   
            Result result = new StreamResult("C:/XMLTemp/Result.xml");
            transformer.transform(source, result); 
        }
    }

    Every suggestion is welcomed.