Solved

parse a simple xml file

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

Independent Software Vendors: 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

Today’s effective marketing is about coming down to the customers’ level and engaging in a whole new way. A text message is one of the most effective and influential ways that you can engage your customers. Here are eight ways that you can utilize t…
Let’s face it: one of the reasons your organization chose a SaaS solution (whether Microsoft Dynamics 365, Netsuite or SAP) is that it is subscription-based. The upkeep is done. Or so you think.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

689 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