Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 17th, 2004, 03:26 AM
MD MD is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default xml in java


I have started with the following code for xml in java.It is right now incomplete.

================================================== ==========

import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class try3{


public static void main (String argv []){
try {


DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("try.xml"));

// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


NodeList listOfPersons = doc.getElementsByTagName("RECORD5");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);

}
}
}
================================================
The element RECORD5 has attributes, UNAME and PASSWORD.
How do I access the values of these two attributes.
If I get the value of Node firstPersonNode printed then it gives null value.Why is it so.
I am new to xml in java.So help is highly appreciated
Thanx


 
Old September 17th, 2004, 04:49 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Element nodes don't have a value. I'm not very familiar with the Java implementation of the XML dom api but I would imagine that to get the value of an attribute (attribute nodes do have a value) then you would write:
Code:
System.out.println("UNAME: " + firstPersonNode.getAttribute("UNAME"));
--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 17th, 2004, 05:26 AM
MD MD is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx it works..
How can one parse a xml string in java program?

Regards





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use XML in java krisdasword XML 2 October 10th, 2007 06:28 AM
Regarding Java and XML yaksha@forest Other Programming Languages 1 October 31st, 2006 12:41 PM
XML and Java surendran Java Basics 2 July 4th, 2006 09:41 PM
copy xml to xml (with java script) Umen XML 4 September 14th, 2004 06:51 AM
about XML receive by tomcatand XML parse by java taianmhzy Servlets 0 May 20th, 2004 01:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.