Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

parse a simple xml file

Posted on 2006-06-18
2
193 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference between member and local variables. 29 140
Fisheye tool 2 116
java beans and EJBs 5 196
HTML5 vs JSP - REST based application development 4 1,190
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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