Solved

parse a simple xml file

Posted on 2006-06-18
2
186 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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

12 Experts available now in Live!

Get 1:1 Help Now