Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830
this week in the
Programmer Certification
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
XML and Related Technologies
Why I can't retrieve records stored in a XMLFile correctly?
Mellihoney Michael
Ranch Hand
Posts: 124
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Why I can't retrieve records stored in a XMLFile correctly?
ReadXMLFile.java
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.DOMException; public class ReadXMLFile{ //Global value so it can be ref'd by the tree-adapter static Document document; public static void main(String argv[]){ //create an input source File xmlFile = new File("address.xml"); //create an instance of document builder that contains a parser DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); //parse and create the in-memory document object document = docBuilder.parse(xmlFile); //get reference to the root document Node rootNode = document.getDocumentElement(); NamedNodeMap nnm1 = rootNode.getAttributes(); System.out.println("The root node is : "+rootNode.getNodeName()+ "\n"); if (nnm1!= null) { for (int i=0;i < nnm1.getLength(); i++) { Node n1 = nnm1.item(i);//0 for the first element? System.out.println("The attribute name is : "+n1.getNodeName()+ " and the attribute value is - "+n1.getNodeValue() +"\n"); } } //get the children of the root element and process ALL the child node. NodeList children = rootNode.getChildNodes(); for (int i=0;i < children.getLength(); i++) { Node n = children.item(i); System.out.println("The child node is : "+n.getNodeName()+ " and the node value is: "+n.getNodeValue()+"\n"); NamedNodeMap nnm2 = n.getAttributes(); if (nnm2!= null) { for (int j=0;j < nnm2.getLength(); j++) { Node n2 = nnm2.item(j); System.out.println("The attribute name is :"+n2.getNodeName()+ "; and the attribute value is - "+n2.getNodeValue() +"\n"); } } } } catch (Exception e) { e.printStackTrace(); } } // main() }//class
the xml file is:
address.xml
<?xml version="1.0" ?> <addressbook version="Ver. 1"> <entry list="personal"> <name>Ms Smith</name> <address>1 Central Street, Sydney</address> <phone>23232323</phone> </entry> <entry list="business"> <name>Mr William</name> <address>1 George Street, Sydney</address> <phone>12312323</phone> </entry> </addressbook>
the output:
The root node is : addressbook The attribute name is : version and the attribute value is - Ver. 1 The child node is : #text and the node value is: The child node is : entry and the node value is: null The attribute name is :list; and the attribute value is - personal The child node is : #text and the node value is: The child node is : entry and the node value is: null The attribute name is :list; and the attribute value is - business The child node is : #text and the node value is:
----------------------------------------------------------------------
But the output is supposed to be
The root node is : addressbook The attribute name is : version and the attribute value is - Ver. 1 The child node is : entry and the node value is: null The attribute name is :list; and the attribute value is - personal The child node is : entry and the node value is: null The attribute name is :list; and the attribute value is - business
can anyone help to explain me?
[ January 14, 2004: Message edited by: Mellihoney Michael ]
a beginner in java
Ray Stojonic
Ranch Hand
Posts: 326
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The #text tags are the closing tags of your nodes.
If you want to work with XML, I'd recommend learning to use SAX and JDOM.
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You are seeing a lot of text nodes that contain the crlf and spaces of your formatted XML. Rather than index through the child nodes, consider use of the getElementsByTagName method to go directly to the Elements you want.
Bill
Mark Spritzler
ranger
Posts: 17347
11
I like...
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am going to move this
thread
to the XML forum. This forum is for
Servlet
questions.
Thanks
Mark
Perfect World Programming, LLC
-
iOS Apps
How to Ask Questions the Smart Way FAQ
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
XPath and Modified DOM document object...
XML Node Parsing Problem , when Node owns NodeList using Xercer , HELP !!!!
a ReadXMLFile Servlet works without my expectation...
help!Urgent,my assignment.....
Can anyone help?I still can't append records in xml...
More...