Java
--
Questions
--
Followers
Top Experts
<name>xyz</test>
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
the following has an example of how to get things done with xsl. http://www.exampledepot.com/egs/javax.xml.transform/BasicXsl.html
  import java.io.*;
  import javax.xml.parsers.*;
  import org.w3c.dom.*;
  import org.xml.sax.*;
 Â
  public class BasicDom {
    public static void main(String[] args) {
      Document doc = parseXmlFile("infilename.x
      // Obtain a node
      Element element = (Element)doc.getElementsBy
      // set the textContent
      element.setTextContent("my
      // write to a file
      // refer to http://java.sun.com/webservices/jaxp/dist/1.1/docs/tutorial/xslt/2_write.html
    }
 Â
    // Parses an XML file and returns a DOM document.
    // If validating is true, the contents is validated against the DTD
    // specified in the file.
    public static Document parseXmlFile(String filename, boolean validating) {
      try {
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.new
        factory.setValidating(vali
 Â
        // Create the builder and parse the file
        Document doc = factory.newDocumentBuilder
        return doc;
      } catch (SAXException e) {
        // A parsing error occurred; the xml input is not valid
      } catch (ParserConfigurationExcept
      } catch (IOException e) {
      }
      return null;
    }
  }
the ccorrect xml is
<name>xyz</name>






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
   element.setTextContent("my
with the following:
Node parent = element.getParentNode();
parent.removeChild(element
element.setTextContent("my
parent.appendChild(element
GetRootNode.java:43: cannot resolve symbol
symbol  : method setTextContent (java.lang.String)
location: interface org.w3c.dom.Element
element.setTextContent("my

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import java.net.URL;
import java.lang.Object;
import java.io.IOException;
import java.io.*;
import javax.xml.parsers.Document
import javax.xml.parsers.Document
import javax.xml.transform.Source
import javax.xml.transform.Transf
import javax.xml.transform.Transf
import javax.xml.transform.dom.DO
import javax.xml.transform.stream
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class GetRootNode{
 public static void main(String[] args) {
  try{
   BufferedReader bf = new BufferedReader(new InputStreamReader(System.i
   File file = new File("filename.xml");
   if (file.exists()){
    DocumentBuilderFactory fact = DocumentBuilderFactory.new
    DocumentBuilder builder = fact.newDocumentBuilder();
    Document doc = builder.parse("filename.xm
    Node node = doc.getDocumentElement();
    String root = node.getNodeName();
    System.out.println("Root Node: " + root);           Â
      Element element = (Element)doc.getElementsBy
     Â
Node parent = element.getParentNode();
parent.removeChild(element
element.setTextContent("my
parent.appendChild(element
      System.out.println("Node Name"+ element.getNodeName());
          Â
      TransformerFactory tranFactory = TransformerFactory.newInst
  Transformer aTransformer = tranFactory.newTransformer
 Â
  Source src = new DOMSource(doc);
 Â
  javax.xml.transform.Result
  aTransformer.transform(src
            System.out.println("Change
                  System.out.println("ya "+element.getChildNodes().
                  System.out.println("no "+element.getChildNodes())
                           Â
   }
   else{
    System.out.println("File not found!");
   }
  }
  catch(Exception e){}
 }
}






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
<ADDRESS>
     <CITY>
           abc city
     </CITY>
</ADDRESS>
  getElementsByTagName("NAME
if there are more, you will have to iterate over them
read http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Element.html, specifically getElementsByTagName()
element.setNodeValue("my new text"); Â does not work. I tried that yesterday also. is there any other work around?

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Also, if its possible, can you upgrade to java 1.5?
 Text text = doc.createTextNode("test12
 text.setData("hello");
 element.appendChild(text);
Node parent = element.getParentNode();
parent.removeChild(element
Text text = doc.createTextNode("test12
text.setData("hello");
parent.appendChild(text);






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Element element = (Element)doc.getElementsBy
Node parent = element.getParentNode();
parent.removeChild(element
Text text = doc.createTextNode(element
text.setData("hello");
parent.appendChild(text);
Then use the original method that i stated above, namely setTextContent ...

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Java
--
Questions
--
Followers
Top Experts
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.