Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Replacing Element with text using Java iwth XPath

843834May 20 2007 — edited May 21 2007
Hi
I have to relpace elements which are with name <Link>,with
string <?abc id='10' city='xxx' ?>
<root>
 <title>Dummy title</title>

<content type='html'>
<Link id='100,101'/> Dummy <a href='www.google.com'>content</a> that the <a href='www.yahoo.com'>cross</a> linking tool will manipulate
</content>

<content type='html'>
<Link id='200,201'/> Dummy <a href='www.google.com'>content</a> that the <a href='www.yahoo.com'>cross</a> linking tool will manipulate
</content>

<content type='html'>
<Link id='300,301'/> Dummy <a href='www.google.com'>content</a> that the <a href='www.yahoo.com'>cross</a> linking tool will manipulate
</content>
<removed>500</removed_ids>
</root>
expected out put is file is

code]<root>
<title>Dummy title</title>

<content type='html'>
<?abc id='100,101' city='xxx' ?>Dummy content that the cross linking tool will manipulate
</content>

<content type='html'>
<?abc id='200,201' city='xxx' ?>Dummy content that the cross linking tool will manipulate
</content>

<content type='html'>
<?abc id='300,310' city='xxx' ?> Dummy content that the cross linking tool will manipulate
</content>
<removed>500</removed_ids>
</root>

java code is
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(new File("C:\\LinkProcess.xml"));
    XPath xpath = XPathFactory.newInstance().newXPath();
    String expression = "//Link";
    NodeList  nodelist = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
    for(int i=0;i<nodelist.getLength();i++){
    Element element = (Element)nodelist.item(i);
    String id = element.getAttribute("id");
    System.out.println("id = "+id);
    Text text = document.createTextNode("<?dctm id ='100'?>");
    document.insertBefore(text,element);
    System.out.println("remove is success");
    }
document.insertBefore(text,element); statement throwing exception as
org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.

and i would like to remove element named <removed>
can you help in this

Thanks & Regards
vittal
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 18 2007
Added on May 20 2007
4 comments
434 views