Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: XML get/set node by attribute value

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default XML get/set node by attribute value

    Hello community,

    I've seen several posts regarding XML processing with Java, however none that I've found address my current problem.

    I've developed an XMLFileHandler class that loads a given xml file into a Document object, and supports some basic methods such as getRootElement(), getChildNode(), getChildNodes(), getNodeAttributeValue(), etc., etc., etc....

    This behaviour has been sufficient enough for accessing nodes/values/attributes in the XML document, but my client has now added a requirement to enable editing of some of these values within the XML document.

    I've tried (and failed) at developing a method to do this. The XML file is in the following format:

    <?xml version="1.0" encoding="UTF-8" ?>
    <locations>
       <location id="1" name="location_name">
          <a>some text</a>
          <b>some other text</b>
       </location>
       ...
       ...
       ...
    </locations>

    Where the attributes 'id' AND 'name' can (each) uniquely identify EVERY 'location' node.
    So, the required functionality would be to design a method that can get a 'location' node by a given 'location_name' attribute value, and then set both the 'a' and 'b' sub-node values, and save the changes to the XML file.

    Any suggestions or pointers would be great!
    Thanks for your help in advance.
    Cheers.


  2. #2
    Junior Member
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: XML get/set node by attribute value

    I was able to solve this using XPath:

    1) evaluate an XPath statement that returns the node in question ("//location[@name='value']")
    2) iterate the returned NodeList object to access the nodes 'ID' attribute (must be defined as ID in referenced .dtd)
    3) Element location = document.getElementById( id_from_above_step );
    4) iterate the 'location' element's child nodes
    5) if the node name is equal to that I'm searching for, .setTextContent( "some_new_value" );
    6) save changes to file via Transformer object

    Hoping this may help others in the future.
    Cheers

Similar Threads

  1. How to change JTree's node icon manually?
    By LeonLanford in forum AWT / Java Swing
    Replies: 2
    Last Post: July 27th, 2010, 03:28 AM
  2. Java Newbie: How to Code Node and its Neighbors?
    By ke3pup in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 01:00 AM
  3. TreeNode vs. Node
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 06:06 AM
  4. Printing A TreeNode/Node To Console..
    By Kumarrrr in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 17th, 2010, 12:35 AM
  5. Xml-Node Retrieval
    By prasb in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 4th, 2009, 12:44 PM