Results 1 to 9 of 9
  1. #1
    Omarero is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default save text into xml in java

    how can i save text in xml from java "net beans", for example take from the user the first and last name in text boxes in java and save them into xml file
    <first name>jack</first name>
    <last name>jones</last name>

    thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Omarero is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default

    am still searching, didnt try anything yet.
    i'll look up the sax.
    can you send me any useful links

    thanks much

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Here is a link for SAX project. All what you need is there.

    SAX

    Just read and see. If you stuck with something I'll help you. First make an attempt yourself. ;)

  5. #5
    Omarero is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default

    thanks; am reading the link now , but till now i couldnt find the part where i can save text from java to xml.
    some one suggested DOM

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Only thing you have to do is,

    Java Code:
                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document document = builder.parse(xml_file_path);
                
                XPath xpath = XPathFactory.newInstance().newXPath();
                XPathExpression expression = xpath.compile(xml_tag_name);
                Node aNode = (Node) expression.evaluate(document, XPathConstants.NODE);
                
                // Update the attribute value
                aNode.setTextContent(xml_updating_value);

  8. #8
    Omarero is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default

    thanks much 4 ur effort, i tried it and its nearly solved.
    this is the xml am using :

    <?xml version="1.0"?>

    <bookmark>
    <website>
    <url>[/goal/English football - football gossip - football transfers - football scores[/url] </url>
    <title>goal </title>
    <description>football-site</description>
    </website>
    </bookmark>

    i want to add new url, title and description , which the user inserts in text boxes, so i tried ur code with slight updates:

    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBu ilder();
    Document document = builder.parse("c://bookmark.xml");

    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expression = xpath.compile("url");
    Node aNode = (Node) expression.evaluate(document, XPathConstants.NODE);

    // Update the attribute value
    aNode.setTextContent(newURL);

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You cannot compile the xpath like this.

    Java Code:
    XPathExpression expression = xpath.compile("url");
    Did you read the Java doc related to my code segment? If so you can solve it so easily. You should compile the xpath as follows.

    Java Code:
    XPathExpression expression = xpath.compile("//url");

Similar Threads

  1. Replies: 14
    Last Post: 12-07-2010, 10:41 AM
  2. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 08:31 PM
  3. Replies: 7
    Last Post: 08-17-2010, 06:00 PM
  4. Writing text into an image and save it
    By elcapi in forum Java 2D
    Replies: 6
    Last Post: 09-18-2009, 05:47 PM
  5. Simple: Text to an image & save.
    By cilaes in forum Java 2D
    Replies: 4
    Last Post: 09-06-2008, 06:16 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
  •