Merging xml file using java NodeList

后端 未结 3 1195
悲&欢浪女
悲&欢浪女 2020-12-06 12:20

I\'m trying to merge two xml files as shown below but i can\'t able to get the desired output please help me thank you

Java code:

DocumentBuilderFac         


        
3条回答
  •  旧巷少年郎
    2020-12-06 12:47

    This solution is for files where you need to iterate and verify something before to merge.

    file1.xml:

        
        
            
                07402
                irreversible
                15666
                07756
            
            
        03063
        irreversible
        00916
        04712
    
    

    file2.xml:

        
        
            00001
            polyphosphate polyphosphohydrolase
             Polyphosphate + n H2O <=> (n+1) Oligophosphate
        
        
            00002
            Reduced ferredoxin:dinitrogen oxidoreductase (ATP-hydrolysing)
             16 ATP + 16 H2O + 8 Reduced ferredoxin <=> 8 e- + 16 Orthophosphate + 16 ADP + 8 Oxidized ferredoxin
            
        
            03063
            cephalosporin-C:2-oxoglutarate aminotransferase
             Cephalosporin C + 2-Oxoglutarate <=> (7R)-7-(5-Carboxy-5-oxopentanoyl)aminocephalosporinate + D-Glutamate
        
        
            07402
            (7R)-7-(4-carboxybutanamido)cephalosporanate amidohydrolase
             (7R)-7-(4-Carboxybutanamido)cephalosporanate + H2O <=> 7-Aminocephalosporanic acid + Glutarate
          
    
    

    Result.xml

        
    
        
            07402
            irreversible
            15666
            07756
        (7R)-7-(4-carboxybutanamido)cephalosporanate amidohydrolase
     (7R)-7-(4-Carboxybutanamido)cephalosporanate + H2O <=> 7-Aminocephalosporanic acid + Glutarate
    
        
            03063
            irreversible
            00916
            04712
        cephalosporin-C:2-oxoglutarate aminotransferase
     Cephalosporin C + 2-Oxoglutarate <=> (7R)-7-(5-Carboxy-5-oxopentanoyl)aminocephalosporinate + D-Glutamate
    
    
    

    Java program to do this:

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.StringWriter;
    import java.io.Writer;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class MergeXML {
    
        public static void main(String[] args) {
            MergeXML m = new MergeXML();
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    
                DocumentBuilder db;
    
                db = dbf.newDocumentBuilder();
    
                Document secondaryMetabolismXML = db
                        .parse(new File("/home/bioinfo/workspace/teste/src/file1.xml"));
                Document generalMetabolismXML = db
                        .parse(new File("/home/bioinfo/workspace/teste/src/file2.xml"));
    
                NodeList secondaryReactions = secondaryMetabolismXML.getElementsByTagName("reaction");
                NodeList generalReactions = generalMetabolismXML.getElementsByTagName("reaction");
    
                for (int s = 0; s < secondaryReactions.getLength(); s++) {
                    Node secondaryReaction = secondaryReactions.item(s);
                    for (int g = 0; g < generalReactions.getLength(); g++) {
                        Node generalReaction = generalReactions.item(g);
                        if (getChildrenByNodeName(secondaryReaction, "ID").getTextContent()
                                .equals(getChildrenByNodeName(generalReaction, "ID").getTextContent())) {
                            if (getChildrenByNodeName(generalReaction, "reactionName") != null) {
                                secondaryReaction.appendChild(secondaryMetabolismXML
                                        .importNode(getChildrenByNodeName(generalReaction, "reactionName"), true));
                            }
                            if (getChildrenByNodeName(generalReaction, "reactionAlternativeName") != null) {
                                secondaryReaction.appendChild(secondaryMetabolismXML.importNode(
                                        getChildrenByNodeName(generalReaction, "reactionAlternativeName"), true));
                            }
                            if (getChildrenByNodeName(generalReaction, "reactionDescription") != null) {
                                secondaryReaction.appendChild(secondaryMetabolismXML
                                        .importNode(getChildrenByNodeName(generalReaction, "reactionDescription"), true));
                            }
                        }
                    }
                }
                TransformerFactory tFactory = TransformerFactory.newInstance();
                Transformer transformer = tFactory.newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    
                DOMSource source = new DOMSource(secondaryMetabolismXML);
                StreamResult result = new StreamResult(new StringWriter());
                transformer.transform(source, result);
    
                Writer output = new BufferedWriter(
                        new FileWriter("/home/bioinfo/workspace/teste/src/Result.xml"));
                String xmlOutput = result.getWriter().toString();
                output.write(xmlOutput);
                output.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * Returns a node child when you have a match with a given node name
         * 
         * @param node
         * @param nodeName
         * @return
         */
        public static Node getChildrenByNodeName(Node node, String nodeName) {
            for (Node childNode = node.getFirstChild(); childNode != null;) {
                Node nextChild = childNode.getNextSibling();
                if (childNode.getNodeName().equalsIgnoreCase(nodeName)) {
                    return childNode;
                }
                childNode = nextChild;
            }
            return null;
        }
    }
    

提交回复
热议问题