mardi 21 juin 2016

Overwriting node contents based on "id" in Java with DOM

Is there a way to overwrite the contents of an element with the contents of another element based on the "id" attribute?

<index xmlns="http://www.newsomeowns.com">
<Hospitals size="standard">
        <Hospital schoolCode="New" codeSet="base" intro="scan" id="0" included="true">
            <text>New Principal</text>
            <lunchAvails>
                <lunchAvail intro="scan" id="0" occurs="1" lunchAvailId="45"/>
                <lunchAvail intro="scan" id="1" occurs="1" lunchAvailId="46"/>
            </lunchAvails>
        </Hospital>
        <Hospital schoolCode="Old" schoolId="4" codeSet="base" intro="scan" id="1" included="true">
            <text>Great Staff</text>
            <lunchAvails>
                <lunchAvail intro="scan" id="0" occurs="1" lunchAvailId="42"/>
                <lunchAvail intro="scan" id="1" occurs="1" lunchAvailId="39"/>
            </lunchAvails>
        </Hospital>



    <schools>   
        <school id="0">
            <text>N/A</text>
        </school>
        <school id="1">
            <text>N/A</text>
        </school>

    </schools>
</index>

I need to search through the schools tab, find the id number and then go to the Hospital tab and the corresponding id.

After that I need the schoolCode to be overwrite "N/A" in back in the schools tab.

Is there a way to do this?

Here is my code so far.

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Input extends schoolId {

    public static void main(String argv[]) {

       try {
        String filepath = "FileName;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);




//=================================================================================================================


        Node school = doc.getElementsByTagName("school").item(0);

        {



        NodeList list = school.getChildNodes();

        for (int i = 0; i < list.getLength(); i++) {

            Node node = list.item(i);



        NamedNodeMap attr = school.getAttributes();
        Node nodeAttr = attr.getNamedItem("text");
        nodeAttr.setTextContent("??");


        System.out.println("");
           }

        }

//=================================================================================================================



        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filepath));
        transformer.transform(source, result);

        System.out.println("XML WRITTEN");

       } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
       } catch (TransformerException tfe) {
        tfe.printStackTrace();
       } catch (IOException ioe) {
        ioe.printStackTrace();
       } catch (SAXException sae) {
        sae.printStackTrace();
       }
    }
}

Aucun commentaire:

Enregistrer un commentaire