When Using DOM can the text in <linkDesc>[url]www.deleteme.com[/url] desc</linkDesc> be replaced or will the link element need to be
Removed and replaced?

<?xml version="1.0" encoding="utf-8"?><links>
<links>

<link action="save">
<linkName>www.deleteme.com name</linkName>
<linkPath>www.deleteme.com</linkPath>
<linkDesc>www.deleteme.com desc</linkDesc>
</link>

</links>

thanks

Recommended Answers

All 3 Replies

which programming language do you use
with xsl no problem

Java and I just started using DOM I need to fight it for a while to learn basics.
I have a working solution. It may be over kill but it ends up rewriting xml
I'll post the example.


original xml
NOTE: The linkDesc will be altered.

<?xml version="1.0" encoding="utf-8"?>
<links>
  <link action="save">
    <linkName>ceyesumma@hotmail.com</linkName>
    <linkPath>ceyesumma@hotmail.com</linkPath>
    <linkDesc>User Email</linkDesc>
  </link>
  <link action="save">
    <linkName>ceyesumma@hotmail.com</linkName>
    <linkPath>ceyesumma@hotmail.com</linkPath>
    <linkDesc>User Email</linkDesc>
  </link>
  <link action="save">
    <linkName>www.hotmail.com name</linkName>
    <linkPath>www.hotmail.com</linkPath>
    [b] <linkDesc>The original description.</linkDesc> [/b]
  </link>
</links>

find the element and save the data in text nodes and introduce new data to the linkDesc tag

private void processForEdit(Element e) throws FileNotFoundException{
        if (e != null) {
            child = e.getChildNodes();




            for (int z = 0; z < child.getLength(); z++) {
                Element element = (Element) child.item(z);
                String name = element.getTagName();
               

                if (element.getNodeName().equals(LINKNAME)) {




                    if (element.TEXT_NODE > 0) {
                        value = getTextValue(child.item(z));
                        if (value != null) {
                            setLinkName(value);
                        }
                    }



                }
                if (element.getNodeName().equals(LINKPATH)) {

                    if (element.TEXT_NODE > 0) {
                        value = getTextValue(child.item(z));
                        if (value != null) {
                            setLinkPath(value);
                        }
                    }


                }
                if (element.getNodeName().equals(LINKDESC)) {

                    setLinkDesc(OpenURLListSelectionPanel.descTxtField.getText());
                 

                }




            }
           
            Document thisDoc = getDoc();
            root.removeChild(e);
            NoteRewriteElementXML nre = new NoteRewriteElementXML();
            nre.appendToOriginalXMl(thisDoc, linkName, linkPath, linkDesc);
        }

    }

change the document,transform the doc and rewrite the xml

public NoteRewriteElementXML() throws IOException{

    }

    public void appendToOriginalXMl(Document doc,String linkName,String linkPath,String linkDesc) throws IOException {

       
      this.doc=doc;

         NoteRewriteElementXML.linkName=linkName;
         NoteRewriteElementXML.linkPath=linkPath;
         NoteRewriteElementXML.linkDesc=linkDesc;

        frame = MusicSystemsJDesktopManager.getCurrentFrame();
        MusicSystemsJDesktopManager.setCurrentFrame(frame);
        frameName = frame.getTitle();
        JTabbedPane pane = (JTabbedPane) frame.getContentPane().getComponent(0);
        int index = pane.getSelectedIndex();
        tabName = pane.getTitleAt(index);

     
       addFragment(doc,linkName,linkPath, linkDesc);
        

        //set up a transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");


        //create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);

        trans.transform(source, result);
        xmlString = result.getWriter().toString();


       
        writeNewXMLFile(systemDir);
       
 
    }
    



      

    

    public static void addFragment(Document doc,String linkName,String linkPath,String linkDesc) {

    

        Element link;
        setDoc(doc);
         root = doc.getDocumentElement();
        DocumentFragment fragment = doc.createDocumentFragment();



        link = makeLinkNode(linkName, linkPath,linkDesc);
        fragment.appendChild(link);
        
        root.appendChild(fragment);
    }

    private static Element makeLinkNode(String name, String url,String desc) {
        Element nameNode = doc.createElement(linkNameElement);
       
        Text nameTextNode = doc.createTextNode(name);
        nameNode.appendChild(nameTextNode);

        Element urlNode = doc.createElement(linkPathElement);
        Text urlTextNode = doc.createTextNode(url);
        urlNode.appendChild(urlTextNode);

        Element descNode = doc.createElement(linkDescElement);
        Text descTextNode = doc.createTextNode(desc);
        descNode.appendChild(descTextNode);

        Element linkNode = doc.createElement(linkElement);
        linkNode.setAttribute("action", action);
        linkNode.appendChild(nameNode);
        linkNode.appendChild(urlNode);
        linkNode.appendChild(descNode);
        return (linkNode);
    }

     private static void writeNewXMLFile(String systemDir) {

        try {
            OutputStream fout = new FileOutputStream(systemDir);
            OutputStream bout = new BufferedOutputStream(fout);
            OutputStreamWriter out = new OutputStreamWriter(bout, "8859_1");
            out.write(xmlDeclaration);
            out.write("<!DOCTYPE links [");
            out.write("<!ELEMENT links (link)+>");
            out.write("<!ELEMENT link (linkName,linkPath,linkDesc)>");
            out.write("<!ATTLIST link action CDATA #REQUIRED>");
            out.write("<!ELEMENT linkName (#PCDATA)>");
            out.write("<!ELEMENT linkPath (#PCDATA)>");
             out.write("<!ELEMENT linkDesc (#PCDATA)>");
            out.write("]>");
            out.write(xmlString);
            out.flush(); //Don't forget to flush!
            out.close();

        } catch (UnsupportedEncodingException e) {
            System.out.println("This VM does not support the Latin-1 character set.");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

revised xml
NOTE: The linkDesc has been altered in the doc and then the xml file.

<?xml version="1.0" encoding="utf-8"?>
<links>
  <link action="save">
    <linkName>ceyesumma@hotmail.com</linkName>
    <linkPath>ceyesumma@hotmail.com</linkPath>
    <linkDesc>User Email</linkDesc>
  </link>
  <link action="save">
    <linkName>ceyesumma@hotmail.com</linkName>
    <linkPath>ceyesumma@hotmail.com</linkPath>
    <linkDesc>User Email</linkDesc>
  </link>
  <link action="save">
    <linkName>www.hotmail.com name</linkName>
    <linkPath>www.hotmail.com</linkPath>
    <linkDesc>The original xml has been altered by changing this description</linkDesc>
  </link>
</links>

output

CLASS 
 public class ReadLinksXML {: 
 --> in private void processForEdit(link)var: (String selection)<-- 
 : 0 : link var : count+AND e.getTagName() : 

CLASS 
 public class ReadLinksXML {: 
 --> in private void processForEdit(link)var: (String selection)<-- 
 : null: action=getAction();: 

CLASS 
 public class ReadLinksXML {: 
 --> in private void processForEdit(link)var: (String selection)<-- 
 : 3:  child = e.getChildNodes();: 

CLASS 
 public class ReadLinksXML {: 
 --> in private void processForEdit(link)var: (String selection)<-- 
 : linkName: String name = (Element) child.item(0); : 

 --> inpublic String getTextValue(linkName) var: n.getNodeName()<-- 

n is a text
CLASS 
 public class ReadLinksXML {: 
 --> inpublic String getTextValue(linkName) var: n.getNodeName()<-- 
 : www.hotmail.com name: value = n.getFirstChild().getNodeValue();: 

CLASS 
 public class ReadLinksXML {: 
 --> in private void processForEdit(link)var: (String selection)<-- 
 : linkPath: String name = (Element) child.item(1); : 

 --> inpublic String getTextValue(linkPath) var: n.getNodeName()<-- 

n is a text
CLASS 
 public class ReadLinksXML {: 
 --> inpublic String getTextValue(linkPath) var: n.getNodeName()<-- 
 : www.hotmail.com: value = n.getFirstChild().getNodeValue();: 

CLASS 
 public class ReadLinksXML {: 
 --> in private void processForEdit(link)var: (String selection)<-- 
 : linkDesc: String name = (Element) child.item(2); : 

CLASS 
 public class ReadLinksXML {: 
 --> in private void processForEdit(link)var: (String selection)<-- 
 : www.hotmail.com name , www.hotmail.com , The original xml has been altered by changing this description :  var : linkName, 

linkPath, linkDesc : 

 --> in public CreateSystemStartFolders() var: xxxxxxxxx : xxxxxxxx<-- 

CLASS 
public class NoteRewriteElementXML extends CreateSystemStartFolders implements java.io.Serializable { {: 
 --> in   public NotesAddNewLinkToOriginalXML(){ var: xxxxxxxxx : xxxxxxxx<-- 

 --> in  public void appendToOriginalXMl(www.hotmail.com name , www.hotmail.com , The original xml has been altered by 

changing this description) { var: (String linkName,String linkPath,String linkDesc)<-- 

 --> in public static void setCurrentFrame(frame1) var: currentFrame<-- 

CLASS 
  public static class MusicSystemsJDesktopManager extends DefaultDesktopManager {: 
 --> in public static void setCurrentFrame(frame1) var: currentFrame<-- 
 : frame1: currentFrame: 

 --> in  public static void addFragment(www.hotmail.com name , www.hotmail.com , The original xml has been altered by 

changing this description) var: Document doc,String linkName,String linkPath,String linkDesc: xxxxxxxx<-- 

Here's the  the original Document : It does nest the new link 

<links>
<link action="save">
<linkName>ceyesumma@hotmail.com</linkName>
<linkPath>ceyesumma@hotmail.com</linkPath>
<linkDesc>User Email</linkDesc>
</link>
<link action="save">
<linkName>ceyesumma@hotmail.com</linkName>
<linkPath>ceyesumma@hotmail.com</linkPath>
<linkDesc>User Email</linkDesc>
</link>
<link action="save">
<linkName>www.hotmail.com name</linkName>
<linkPath>www.hotmail.com</linkPath>
<linkDesc>The original xml has been altered by changing this description</linkDesc>
</link>
</links>

 --> in public static synchronized ResourceBundle getResources() var: xxxxxxxxx : xxxxxxxx<-- 

 --> in  ublic void loadURLDescXML() var: xxxxxxxxx : xxxxxxxx<-- 

 --> in public static synchronized ResourceBundle getResources() var: xxxxxxxxx : xxxxxxxx<-- 

 --> in public static readXML(C:\Users\Steves_\.targetFolders\admin_\admin\Note Folder\frame1\1tab1\url\tabLinks.xml) var: 

(String xmlName)<-- 

 --> in public void readAllForURLDesc(www.hotmail.com name) var: x<-- 

 --> in   private void parseDocumentForURLReference(www.hotmail.com name)var: ()<-- 

CLASS 
 public class ReadLinksXML {: 
 --> in   private void parseDocumentForURLReference(www.hotmail.com name)var: ()<-- 
 : 3: NodeList nl = root.getElementsByTagName(LINK);: 

CLASS 
 public class ReadLinksXML {: 
 --> in   private void parseDocumentForURLReference(www.hotmail.com name)var: ()<-- 
 : link: String name = (Element) rl.item(0); : 

 --> inpublic String getTextValue(linkName) var: n.getNodeName()<-- 

n is a text
CLASS 
 public class ReadLinksXML {: 
 --> inpublic String getTextValue(linkName) var: n.getNodeName()<-- 
 : ceyesumma@hotmail.com: value = n.getFirstChild().getNodeValue();: 

CLASS 
 public class ReadLinksXML {: 
 --> in   private void parseDocumentForURLReference(www.hotmail.com name)var: ()<-- 
 : link: String name = (Element) rl.item(1); : 

 --> inpublic String getTextValue(linkName) var: n.getNodeName()<-- 

n is a text
CLASS 
 public class ReadLinksXML {: 
 --> inpublic String getTextValue(linkName) var: n.getNodeName()<-- 
 : ceyesumma@hotmail.com: value = n.getFirstChild().getNodeValue();: 

CLASS 
 public class ReadLinksXML {: 
 --> in   private void parseDocumentForURLReference(www.hotmail.com name)var: ()<-- 
 : link: String name = (Element) rl.item(2); : 

 --> inpublic String getTextValue(linkName) var: n.getNodeName()<-- 

n is a text
CLASS 
 public class ReadLinksXML {: 
 --> inpublic String getTextValue(linkName) var: n.getNodeName()<-- 
 : www.hotmail.com name: value = n.getFirstChild().getNodeValue();: 

CLASS 
 public class ReadLinksXML {: 
 --> in   private void parseDocumentForURLReference(www.hotmail.com name)var: ()<-- 
 : linkName:el.getTagName() : 

 -->  public void processForURLDesc(link) var: ( Element e)<-- 

 --> inpublic String getTextValue(linkPath) var: n.getNodeName()<-- 

n is a text
CLASS 
 public class ReadLinksXML {: 
 --> inpublic String getTextValue(linkPath) var: n.getNodeName()<-- 
 : www.hotmail.com: value = n.getFirstChild().getNodeValue();: 

CLASS 
 public class ReadLinksXML {: 
 -->  public void processForURLDesc(link) var: ( Element e)<-- 
 : www.hotmail.com: strValue: 

 --> inpublic String getTextValue(linkDesc) var: n.getNodeName()<-- 

n is a text
CLASS 
 public class ReadLinksXML {: 
 --> inpublic String getTextValue(linkDesc) var: n.getNodeName()<-- 
 : The original xml has been altered by changing this description: value = n.getFirstChild().getNodeValue();: 

CLASS 
 public class ReadLinksXML {: 
 -->  public void processForURLDesc(link) var: ( Element e)<-- 
 : The original xml has been altered by changing this description: strValue:
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.