Results 1 to 6 of 6
  1. #1
    oliveira is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default 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?

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by oliveira View Post
    But this cleans all the elements value including the name DirTrace.
    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:
    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("");
    			}
    		}
    (not tested)

    Or i have an error in reasoning with the xpath expression :confused:

  3. #3
    oliveira is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default 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

  4. #4
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by oliveira View Post
    Well i tried your version but ain´t still working, it cleans all of child nodes content, with the element node 'value'.
    are you sure, that this behavior doesn`t come from somewhere else ?
    Can you poste all of your code? Or can you give us an full runnable example ?

  5. #5
    oliveira is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default 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.

  6. #6
    oliveira is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default 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

  1. Replies: 1
    Last Post: 02-23-2010, 09:48 AM
  2. How to get a node value of an XML element?
    By rsenth99 in forum Java Servlet
    Replies: 9
    Last Post: 02-15-2010, 11:35 AM
  3. how can we get the element of by using the hashtable
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 05-06-2008, 01:45 PM
  4. a no such element exception
    By headlice1 in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •