Results 1 to 6 of 6
Thread: remove element contents
- 12-06-2010, 04:41 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 15
- Rep Power
- 0
remove element contents
Hello to all, it's my first post here so excuse me if i make any mistake....:)
So this is it, i have this xml:
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" name="MZTBroker/DCS/BW_DCS_2">
<repoInstanceName>%%DOMAIN%%-BW_DCS_2</repoInstanceName>
<maxdeploymentrevision>5</maxdeploymentrevision>
<NVPairs name="Global Variables">
<NameValuePair> <name>BusinessDomainVariables/DCS/BusinessDomainResources/Interfaces/retireveMealorderSABRE/inputQueue</name>
<value>BRKTS.DT.REQUEST</value>
</NameValuePair>
<NameValuePair>
<name>DirTrace</name>
<value>/logs/tibco/MZTBroker/DCS</value>
</NameValuePair>
</NVPairs>
I wan't to clean the value content but only from elements that contains the name BusinessDomainVariables, so i have this code to do that.
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate(""//NVPairs[@name='Global Variables']/*/name[contains(.,'BusinessDomainVariables')]/value"", doc,XPathConstants.NODESET);
for (int idx = 0; idx < nodes.getLength(); idx++) {
nodes.item(idx).setTextContent("");
}
But this cleans all the elements value including the name DirTrace.
Can anybody help me?
- 12-06-2010, 06:17 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
I find that strange. I would expected, that the NodeList nodes is empty, because the xpath expression doesnt match or? I believe that, because, value isnt a child of name...
With "//NVPairs[@name='Global Variables']/*/name[contains(.,'BusinessDomainVariables')]" you would get all name elements which contains "BusinessDomainVariables". But these nodes havent any childs! value is a childnode of NameValuePair!
Mhm.. my suggestion would be, to do the contains check with java ?!
maybe something like:
(not tested)Java Code:NodeList nodes = (NodeList) xpath.evaluate("//NVPairs[@name='Global Variables']/NameValuePair/*", doc,XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i += 2) { if (nodes.item(i).getTextContent().contains("BusinessDomainVariables")) { nodes.item(i + 1).setTextContent(""); } }
Or i have an error in reasoning with the xpath expression :confused:
- 12-06-2010, 06:34 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 15
- Rep Power
- 0
remove element contents
Hello, thank you very much for yours reply.
Well i tried your version but ain´t still working, it cleans all of child nodes content, with the element node 'value'.
Maybe i have to make a condition like
if(name element contains 'BussinessDomainVariables')
then
cleans value element content
else
name doesn't contains 'BussinessDomainVariables'.
I gess it's the only way
- 12-06-2010, 06:38 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
- 12-07-2010, 07:49 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 15
- Rep Power
- 0
Can you poste all of your code? Or can you give us an full runnable example ?
This is the part where it matches if a file exists, and then evaluate to the path example.
Note: There are parts here in the code that are not being used, that's because first i tried searching for the path without xpath, and so i let that way.....If you need somthing else please tell me...and thank you very much for your attention to my doubts.....
if(file.exists()){
System.out.println("Elemento Raíz: " + doc.getDocumentElement().getNodeName());
Element root = doc.getDocumentElement();
NodeList attributes = root.getElementsByTagName("NVPairs");
//if (root.getTagName() == ""){
if(root.getAttribute("Global Variables") != null){
System.out.println("Iterando sobre todos omsos elementos filhos do Elemento NVPairs com o atributo Global Variables");
for (int s = 0; s < attributes.getLength(); s++) {
String value = "Global Variables";
String attribute = "name";
Node fstNode = attributes.item(s);
//while (fstNode != null && fstNode.getNodeType() != Node.ELEMENT_NODE)
if (fstNode != null && fstNode.getNodeName() == "NVPairs") {
if (getAttribute((Element) attributes.item(s), attribute).equals(value)){
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//NVPairs[@name='Global Variables']/*/name[contains(.,'BusinessDomainVariables')]/value");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeName());
expr = xpath.compile("//NVPairs[@name='Global Variables']/*/name[contains(.,'BusinessDomainVariables')]/value");
// Selecting all person element descendant's value
result = expr.evaluate(doc, XPathConstants.NODESET);
nodes = (NodeList) result;
for (int idx = 0; idx < nodes.getLength(); idx++) {
nodes.item(idx).setTextContent("");
}
}
//NodeList nodes = (NodeList) xpath.evaluate("//NVPairs[@name='Global Variables']/*/name[contains(.,'BusinessDomainVariables') or (.,'SharedVariables')]/value", doc,XPathConstants.NODESET);
}
else
System.out.println("A tag não existe.");
//if("NVPairs".equals(fstNode.getNodeName())){
}
else
System.out.println("A tag não existe");
}
}
else
System.out.println("Não existem atributos com o nome Global Variables");
// }else
//System.out.println("Não existem elementos com o nome NVPairs");
//Guardar ficheiro com as modificações
//TransformerFactory transformerFactory = TransformerFactory.newInstance();
//Transformer transformer = transformerFactory.newTransformer();
//DOMSource source = new DOMSource(doc);
//StreamResult result = new StreamResult(new File(xmlFile));
//transformer.transform(source, result);
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
//to generate output to console use this serializer
//XMLSerializer serializer = new XMLSerializer(System.out, format);
//to generate a file output use fileoutputstream instead of system.out
XMLSerializer serializer = new XMLSerializer(
new FileOutputStream(new File("C://Users/Consultor/Downloads/BW_DCS_2_deploy.xml")), format);
serializer.serialize(doc);
System.out.println("Done");
}
And this is my xml document:
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" name="MZTBroker/DCS/BW_DCS_2">
<repoInstanceName>%%DOMAIN%%-BW_DCS_2</repoInstanceName>
<maxdeploymentrevision>5</maxdeploymentrevision>
<NVPairs name="Global Variables">
<NameValuePair>
<name>BusinessDomainVariables/DCS/BusinessDomainResources/Interfaces/retireveMealorderSABRE/inputQueue</name>
<value>BRKTS.DT.REQUEST</value>
</NameValuePair>
<NameValuePair>
<name>BusinessDomainVariables/DCS/BusinessDomainResources/Interfaces/retireveMealorderSABRE/maxSession</name>
<value>1</value>
</NameValuePair>
<NameValuePair>
<name>BusinessDomainVariables/DCS/BusinessDomainResources/Interfaces/retrieveFlightInfoRequest/handleFlightInfoRequestPub</name>
<value>DCS.retrieveFlightInfoRequest.SUB.BRG</value>
</NameValuePair>
<NameValuePair>
<name>BusinessDomainVariables/DCS/BusinessDomainResources/Interfaces/retrieveMealOrderCATPOR/inputQueue</name>
<value>BRKTS.DT.CPREQUEST</value>
</NameValuePair>
<NameValuePair>
<name>BusinessDomainVariables/DCS/BusinessDomainResources/Interfaces/retrieveMealOrderCATPOR/maxSessions</name>
<value>1</value>
</NameValuePair>
<NameValuePair>
<name>BusinessDomainVariables/DCS/BusinessDomainResources/Interfaces/updateMealOrderSABRE/inputQueue</name>
<value>BRKTS.DT.OPSINFO</value>
</NameValuePair>
</NVPairs>Last edited by oliveira; 12-07-2010 at 07:51 AM.
- 12-07-2010, 08:44 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 15
- Rep Power
- 0
remove element contents
Hello again:)
it worked:) the first code you put in here worked allright...
##################################################
NodeList nodes = (NodeList) xpath.evaluate("//NVPairs[@name='Global Variables']/NameValuePair/*", doc,XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i += 2) {
if (nodes.item(i).getTextContent().contains("Business DomainVariables")) {
nodes.item(i + 1).setTextContent("");
}
##################################################
The thing was that the file he was opening allready had the values cleaned...this was a file i already had cleaned the values and he was getting the file so i thaught he was cleaning all but no...i'm really sorry for my mistake, but the thing is that the code you put here did helped a lot.
Thank you very much once again for your concern:)
Similar Threads
-
How to get(copy) the contents i.e the contents in the cells of an excel sheet
By johnvarg in forum AWT / SwingReplies: 1Last Post: 02-23-2010, 09:48 AM -
How to get a node value of an XML element?
By rsenth99 in forum Java ServletReplies: 9Last Post: 02-15-2010, 11:35 AM -
how can we get the element of by using the hashtable
By raj reddy in forum Web FrameworksReplies: 1Last Post: 05-06-2008, 01:45 PM -
a no such element exception
By headlice1 in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:36 PM


LinkBack URL
About LinkBacks


Bookmarks