Replacing Element with text using Java iwth XPath

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 <a href='www.google.com'>content</a> that the <a href='www.yahoo.com'>cross</a> linking tool will manipulate

</content>

<content type='html'>

<?abc id='200,201' city='xxx' ?>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'>

<?abc id='300,310' city='xxx' ?> 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>[/code]

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

[3584 byte] By [vittal.reddya] at [2007-11-27 4:55:17]
# 1

1. This element makes your XML document malformed:<removed>500</removed_ids>

The name of the start tag and the closing tag must always be identical.

http://www.w3.org/TR/REC-xml/#sec-starttags

2. The type of the nodes with which you would like to replace your <Link> elements is called "ProcessingInstruction". Processing instructions are to be handled somewhat differently from elements.

3. The string "><?dctm id ='100'?>" is misspelled. Additionally, it is meant to be a processing instruction so it should not be created as a text node.

4. You use a node list to replace elements by means of a loop. A loop is only useful when there is a system to the data to be processed, which is not the case with your processing instructions:<?abc id='100,101' city='xxx'?>

<?abc id='200,201' city='xxx'?>

<?abc id='300,310' city='xxx'?>

From the fact that you use a loop, I presume that the third node is meant to be <?abc id='300,301' city='xxx' ?>. If so, you can use this code: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);

float n;

String piData;

ProcessingInstruction pi;

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

Element element = (Element)nodelist.item(i);

n = (i+1)*100+((i+1)*100+1)/1000F;

piData = "id='" + n + "' city='xxx'";

pi = document.createProcessingInstruction("abc", piData);

element.getParentNode().replaceChild(pi, element);

System.out.println("Element ><Link...> has been replaced with <?abc " + piData + "?>");

}

This code is not ideal, but I hope is easy to understand and you will be able to change it to suit your needs.

5. To remove an element use the removeChild() method of the Node interface. If your XML document contains "ignorable" white spaces, it's best to use an XPath expression again ("root/removed") to set reference to the node to be removed rather than locate it by position (e.g. using the getLastChild() method).

prgguya at 2007-7-12 10:10:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thanks code works for me using jdk1.5,

i would like to have compatability with jdk1.4.2_05.

for this plat form, i am getting compalation error

XPath xpath = XPathFactory.newInstance().newXPath();

String expression = "//Link";

NodeList nodelist = (NodeList) xpath.evaluate(expression, document,

XPathConstants.NODESET);

vittal.reddya at 2007-7-12 10:10:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

That compilation error should have been known to you before sending your first post but you made no mention of it. Did you not try to compile your code sample or did you want to use JDK 1.5 and change your mind in the meantime? Whichever was the case, the first step should be to decide what you want and then think over the viable alternatives. The latter involves trying to compile and run your code on the same version of JDK that you are planning to use. If you insist on using JDK 1.4.2_05, you can use XSLT stylesheets which support XPath even in JDK 1.4.2_05, or supplement your JDK with a third party class library that supports XPath independently of the JAXP 1.5 API, like Jaxen (used as the default implementation also by JDOM and DOM4J) and Saxon (implementing the javax.xml.xpath.XPathFactory class of version 1.3).

prgguya at 2007-7-12 10:10:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Hi

I tested to see weather i will achive expected functionality with jdk1.5, with this working fine as expected.

Our production server is with jdk1.4.2_05, i am getting compile time exception for few statements as specified above.

jdk1.4.2_05 is having package for XPath, import org.apache.xpath.*

I am not well which method, i have to use to work it out.

can you guide me

Thanks

vittal

vittal.reddya at 2007-7-12 10:10:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...