Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6254413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:10:31+00:00 2026-05-24T14:10:31+00:00

i have to write to the tag which has attribute child as my xml

  • 0

i have to write to the tag which has attribute child as

my xml :

<?xml version="1.0" encoding="UTF-8" standalone="no"?><tree>
<declarations>
<attributeDecl name="name" type="String"/>
</declarations>
<branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value=""/></branch>
<branch>
<attribute name="name" value=""/></branch>
<attribute name="name" value="Do you have this condition for more than a weeks time"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have watery stool"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Excess Stool"/></branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value="Do you have chills"/>
<branch>
<attribute name="name1" value=""/>
</branch>
<branch>
<attribute name="name2" value=""/>
</branch>



</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have high fever for more than a weeks time "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Was your peak temperature greater than 104"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="High Fever"/></branch>
<branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value=""/>
</branch>
<branch>
<attribute name="name" value=""/></branch>
<attribute name="name" value="Duration of the headache spans for more than a day "/>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have headache more than 5 times a day "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have pain in the left part of the head "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Bodyache"/></branch>
<attribute name="name" value="Malaria"/></branch>
</tree>

my code :

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlQuestionwrite {
    Document dom = null;
    Node node = null;
    String filepath = "data/chi-ontology.xml";
    String itemclickedonapplet = null;
    String consent = "Yes";

    public void questionWrite() {
//      String attributechange = Question;
//      String consentvalue = consent;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            dom = db.parse(filepath);
            Element docele = dom.getDocumentElement();
//          Node branch = dom.createElement("branch");
//          Element attribute = dom.createElement("attribute");
            NodeList nl = docele.getElementsByTagName("attribute");
            if (nl != null && nl.getLength() > 0) {
                for (int i = 0; i < nl.getLength(); i++) {
                Element sl = (Element) nl.item(i);
                String val = sl.getAttribute("value").trim();
                if (val.equals("Do you have chills")) {
//              Node parentnode = sl.getParentNode();
                NodeList ashu = sl.getParentNode().getChildNodes();
                for (int i1 = 0; i1 < ashu.getLength(); i1++) {
                    Node node = ashu.item(i1);
                    if (node.getNodeType() != Node.ELEMENT_NODE)
                        continue;
                    Element ele = (Element) ashu.item(i1);
                    if (ele.getNodeName().equals("branch")){
                        NodeList questionappend=ele.getElementsByTagName("attribute");
                        for (int count = 0; i < questionappend.getLength(); count++){
                             Element questionitem = (Element) questionappend.item(i);
                             String appendvalue = sl.getAttribute("name").trim();
                             if (appendvalue.equals("name1") && consent.equals("Yes") ) {
                                 questionitem.setAttribute("value", "this is the question for the yes item "); 
                            }
                             else if(appendvalue.equals("name2") && consent.equals("No")){
                                 questionitem.setAttribute("value", "this is the question for the no item "); 
                             }

                        }


                    }

                }

                }

                }
            }// if ends here 
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            DOMSource source = new DOMSource(dom);
            StreamResult result = new StreamResult(new File(filepath));
            transformer.transform(source, result);

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }

    }

    public static void main (String args []) {
        XmlQuestionwrite qwrite= new XmlQuestionwrite();
        qwrite.questionWrite();
    }
}

Please help me on that .

Thanks dma_K ,it was fun to explore xpath , actually for my convenience i named the name attribute of attribute as name1 and name2

in the real real xml only differentiating factor is the sibling node name “Do you have chills”

so the real xml will look somrthing like with code to set attribute in the attribute tag of

test.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><tree>
<declarations>
<attributeDecl name="name" type="String"/>
</declarations>
<branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value=""/></branch>
<branch>
<attribute name="name" value=""/></branch>
<attribute name="name" value="Do you have this condition for more than a weeks time"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have watery stool"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Excess Stool"/></branch>
<branch>
<branch>
<branch>
**<branch>
<attribute name="name" value="Do you have chills"/>
<branch>
<attribute name="name" value=""/>
</branch>
<branch>
<attribute name="name" value=""/>
</branch>**



</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have high fever for more than a weeks time "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Was your peak temperature greater than 104"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="High Fever"/></branch>
<branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value=""/>
</branch>
<branch>
<attribute name="name" value=""/></branch>
<attribute name="name" value="Duration of the headache spans for more than a day "/>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have headache more than 5 times a day "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have pain in the left part of the head "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Bodyache"/></branch>
<attribute name="name" value="Malaria"/></branch>
</tree>

I have bolded that part off xml which needs to be altered .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:10:32+00:00Added an answer on May 24, 2026 at 2:10 pm

    The approach you suggest is a kind of suicide… More easier and elegant is to use XPath:

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(getClass().getResourceAsStream("test.xml"));
    
    XPath xpath = XPathFactory.newInstance().newXPath();
    
    NodeList nodeList = (NodeList) xpath.evaluate("//branch//attribute", document, XPathConstants.NODESET);
    
    for (int i = 0, len = nodeList.getLength(); i < len; i++) {
        final Element attributeNode = (Element) nodeList.item(i);
    
        String value = attributeNode.getAttribute("name");
    
        if (value.equals("name1")) {
            attributeNode.setAttribute("value", "this is the question for the yes item ");
        }
        else if (value.equals("name2")) {
            attributeNode.setAttribute("value", "this is the question for the no item ");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented an XMLHttpRequest() call to a standalone html page which simply has
I'm working on an application so i have write an dll which contain a
I write an XML file by hand, (i.e. not with LINQ to XML), which
I have the following psudeo html. I need to write some jquery which hides
I have an entry ID, which popularity has to be increased by one. A
I have an application in which there are Course s, Topic s, and Tag
I have a scenario in which I have a class Resource which has two
Ojective: To open config file, look for a particular line which has tag and
I have a site running Plone 4.1 which has a custom content type developed
How can I determine if I have write permission on a remote machine in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.