Solved

parse a simple xml file

Posted on 2006-06-18
2
189 Views
Last Modified: 2010-04-01
Hi,

I would like to parse a simple xml file.
Is this the right way to load it? I will make the file document load dynamic later.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    // Step 2: create a DocumentBuilder
    DocumentBuilder db = dbf.newDocumentBuilder();

    // Step 3: parse the input file to get a Document object
    Document doc = db.parse(new File(C:\\TEST.XML));

In the following xml file i would like to find default tag that = for example test.

Rename the tag.

Add a tag at the highest and lowest level.

And just how to loop throug the tags to find the one i want. Thanks for any help.

Also delete tags.

 <type>
  <default>test1</default>
  <next>testing</next>
  <next>tests</next>
  </type>
- <type>
  <default>test2</next>
  <next>tested</next>
  <next>tester</next>
  </type>
0
Comment
Question by:wilflife
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
FunnyMan earned 500 total points
ID: 16930969
You're on the right track, yes.  Calling doc.getFirstNode() will get you the root Node (tag) of the document.  From there:
node.getChildNodes() -> NodeList of children.
nodelist.getLength() ->  Number of children.
nodelist.item(i) -> Get i-th child of the node (0-based).
node.getNodeName() -> Tag name.
node.getNodeValue() -> Text content of the node.
node.removeChild(n) -> Removes the child node n from node.
node.replaceChild(n1,n2) -> Replaces child node n1 of node with n2.

It looks like Document.renameNode(Node n, String namespaceURI, String qualifiedName) will do renaming for you, but I'm not sure how to use it.

-FM
0
 
LVL 1

Expert Comment

by:FunnyMan
ID: 16931013
For the record, I have yet to figure out WHY the W3C decided on such a roundabout way of implementing this.  It ought to be possible to get the Document by calling a method like XML.parse(), and then get the children by calling node.getLength() and node.getChild(i), but noooo...
-FM
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
default methods vs abstract class 1 109
hashcode is calculated on the basis of value object 2 104
servlet concurrency 13 93
parse example 13 93
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now