Solved

parse a simple xml file

Posted on 2006-06-18
2
194 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
In order to fulfill our mission of inspiring learning in the technology community, Experts Exchange is launching a Course of the Month program. Premium and Team Account members will have access to one course per month as a part of their membership, …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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