  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Tomcat - JSP/Servlet container | | Struts - A MVC web framework | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | JSP - A mailing list about Java Server Pages specification and reference | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | |
Struts & Hibernate
|
|
|
  | | | Fwd: Replacing an element with another element | Fwd: Replacing an element with another element 2007-02-26 - By Mark Drew
Back Hi there first time poster so go easy on me :)
I have a document, that I want to replace an Element with another Element (from a different document). I have tried a number of things but dont seem to be able to get it right. (A little more background, I have a document tree, when you select an Element, you can see the whole Element's text in a text field that you can edit it. When you are done editing that Element, it creates a new document with just that Element, I now want to replace the original element with that text)
Any ideas?
Here is what I have so far... StringBuffer StringBuffer1 = new StringBuffer(editedElementText); ByteArrayInputStream Bis1; try { Bis1 = new ByteArrayInputStream(StringBuffer1.toString ().getBytes("UTF-8 (See http://UTF-8.ora-code.com)")); LineNumberSAXBuilder builder = new LineNumberSAXBuilder(); builder.setValidation(false); Document document = builder.build(Bis1); Element content = (Element)document.getRootElement().clone(); selNode.setElement(content); //the orignial node XMLOutputter outputter = new XMLOutputter(); String modDocument = outputter.outputString(selNode.getElement ().getDocument()); System.out.println(modDocument); } ....
but it doesnt seem to change the original document.
Many thanks in advance
Mark Drew
<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line -break: after-white-space; "><DIV>Hi there<DIV>first time poster so go easy on me :)</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>I have a document, that I want to replace an Element with another Element (from a different document). I have tried a number of things but dont seem to be able to get it right.?</DIV><DIV>(A little more background, I have a document tree, when you select an Element, you can see the whole Element's text in a text field that you can edit it. When you are done editing that Element, it creates a new document with just that Element, I now want to replace the original element with that text)</DIV><DIV><BR class="khtml-block-placeholder"></DIV> <DIV>Any ideas?</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Here is what I have so far...</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><FONT class="Apple-style-span" face= "Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;" >StringBuffer StringBuffer1 = </SPAN></FONT><FONT class="Apple-style-span" color ="#7F0055" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font -size: 11px;">new</SPAN></FONT><FONT class="Apple-style-span" face="Monaco" size ="3"><SPAN class="Apple-style-span" style="font-size: 11px;"> StringBuffer(<FONT class="Apple-style-span" color="#0000C0">editedElementText</FONT></SPAN></FONT> <FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style -span" style="font-size: 11px;">);</SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab -span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white -space:pre"> </SPAN><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">ByteArrayInputStream Bis1;< /SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom : 0px; margin-left: 0px; "><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class ="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><FONT class="Apple-style -span" color="#7F0055" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">try</SPAN></FONT><FONT class="Apple-style-span" face= "Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;"> {< /SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom : 0px; margin-left: 0px; "><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class ="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab -span" style="white-space:pre"> </SPAN><FONT class="Apple-style-span" face= "Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">Bis1 = </SPAN></FONT><FONT class="Apple-style-span" color="#7F0055" face="Monaco" size ="3"><SPAN class="Apple-style-span" style="font-size: 11px;">new</SPAN></FONT> <FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style -span" style="font-size: 11px;"> ByteArrayInputStream(StringBuffer1.toString() .getBytes(</SPAN></FONT><FONT class="Apple-style-span" color="#2A00FF" face= "Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">"UTF-8 (See http://UTF-8.ora-code.com) "</SPAN></FONT><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class ="Apple-style-span" style="font-size: 11px;">));</SPAN></FONT></DIV><DIV style= "margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "> <SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple -tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style= "white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class= "Apple-style-span" style="font-size: 11px;">LineNumberSAXBuilder builder = < /SPAN></FONT><FONT class="Apple-style-span" color="#7F0055" face="Monaco" size= "3"><SPAN class="Apple-style-span" style="font-size: 11px;">new</SPAN></FONT> <FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style -span" style="font-size: 11px;"> LineNumberSAXBuilder();</SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px ; "><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab -span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white -space:pre"> </SPAN><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">builder.setValidation(</SPAN> </FONT><FONT class="Apple-style-span" color="#7F0055" face="Monaco" size="3"> <SPAN class="Apple-style-span" style="font-size: 11px;">false</SPAN></FONT><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">);</SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple -tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style= "white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">Document document = builder.build(Bis1);</SPAN></FONT> </DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin -left: 0px; "><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab -span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white -space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN ><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple -tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style= "white-space:pre"> </SPAN><FONT class="Apple-style-span" face="Monaco" size="3"> <SPAN class="Apple-style-span" style="font-size: 11px;">Element content = (Element)document.getRootElement().clone();</SPAN></FONT></DIV><DIV style= "margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "> <SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple -tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style= "white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><FONT class="Apple-style-span" color="#0000C0" face="Monaco" size= "3"><SPAN class="Apple-style-span" style="font-size: 11px;">selNode</SPAN></FONT ><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style -span" style="font-size: 11px;">.setElement(content); //the orignial node</SPAN> </FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-tab-span" style="white-space:pre"> < /SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab -span" style="white-space:pre"> </SPAN><FONT class="Apple-style-span" face= "Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;" >XMLOutputter outputter = </SPAN></FONT><FONT class="Apple-style-span" color=" #7F0055" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">new</SPAN></FONT><FONT class="Apple-style-span" face="Monaco" size="3"> <SPAN class="Apple-style-span" style="font-size: 11px;"> XMLOutputter();</SPAN>< /FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-tab-span" style="white-space:pre"> < /SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab -span" style="white-space:pre"> </SPAN><FONT class="Apple-style-span" face= "Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">String modDocument = outputter.outputString(</SPAN></FONT><FONT class="Apple-style -span" color="#0000C0" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">selNode</SPAN></FONT><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;"> .getElement().getDocument());</SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple -tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style= "white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">System.</SPAN></FONT><FONT class="Apple-style-span" color="#0000C0" face="Monaco" size="3"><SPAN class="Apple-style-span" style= "font-size: 11px;">out</SPAN></FONT><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;">.println (modDocument);</SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px ; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><SPAN class="Apple -tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style= "white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><BR><SPAN class="Apple-tab-span" style="white-space:pre"> < /SPAN>}</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><SPAN class="Apple-tab-span" style="white -space:pre"> </SPAN>....</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><FONT class="Apple-style-span" face="Monaco" size="3"><SPAN class="Apple-style-span" style="font-size: 11px;"> <BR class="khtml-block-placeholder"></SPAN></FONT></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class= "Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space :pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab -span" style="white-space:pre"> </SPAN><SPAN class="Apple-tab-span" style="white -space:pre"> </SPAN><BR>but it doesnt seem to change the original document.</DIV ><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left : 0px; min-height: 14px; "><BR class="khtml-block-placeholder"></DIV><DIV style= "margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min -height: 14px; ">Many thanks in advance</DIV><DIV style="margin-top: 0px; margin -right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR class ="khtml-block-placeholder"></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; ">Mark Drew?</DIV></DIV ><BR></BODY></HTML> __ ____ ____ ____ ____ ____ ____ ____ ____ ____ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
|
|
 |