• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

XML Node Parsing Problem , when Node owns NodeList using Xercer , HELP !!!!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dear Sirs !
Please , read this mail , do not afraid from the length of it !
It is VERY SIMPLE CODE !!!
I have a some LITTLE problem with Parsing .Please help me !!!
I need to write some method :
public void getDataFromXML(String recievedXMLString) , which tales as parameter some String recievedXMLString , which is already XML Formated like this :
<AddressBook>
<Listing>
<Name>Mr. White</Name>
<Address>
<StreetNum>77</StreetNum>
<StreetName>5th Ave</StreetName>
<City>NY</City>
<State>NY State</State>
<Zip>12345</Zip>
<PhoneNumber>
<AreaCode>123</AreaCode>
<Exchange>exchange</Exchange>
<Number>12345678</Number>
</PhoneNumber>
</Address>
</Listing>
<Listing>
<Name>Mr. Somebody</Name>
<Address>
<StreetNum>77</StreetNum>
<StreetName>5th Ave</StreetName>
<City>NY</City>
<State>NY State</State>
<Zip>12345</Zip>
<PhoneNumber>
<AreaCode>123</AreaCode>
<Exchange>exchange</Exchange>
<Number>12345678</Number>
</PhoneNumber>
</Address>
</Listing>
</AddressBook>
Now i've recently writed a simple code for Parcing (I have used org.xml.sax.* and Xercer ):
(http://xml.apache.org/xerces-j/apiDocs/index.html)
import org.xml.sax.InputSource;
import org.exolab.castor.xml.*;
import org.exolab.castor.mapping.*;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.FileReader;
import java.util.List;
import java.util.Iterator;

try
{

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
ByteArrayInputStream in = new ByteArrayInputStream(recievedXMLString.getBytes()) ;
Document response = builder.parse(in); // here catch a Exceptions
Element responseElement = response.getDocumentElement(); //AddressBook
System.out.print("The name of Document is "+
responseElement.getNodeName()); // out : Address book

NodeList nodeList_Listings = response.getElementsByTagName("Listing");
// take a number of Listings

int numListings = nodeList_Listings.getLength();
System.out.println(" The Curent Document has "+
nodeList_Listings.getLength() +
" listings . \n");

//**************** Walking through the Listings : *******************
for(int i=0; i < numListings ; i++) // Walk through Listings List
{
System.out.println("*********************************************");

System.out.println("The "+ i+1 +" Listing is :");

NodeList dataList = nodeList_Listings.item(i).getChildNodes();

//get a Current Listing here with it's nodes : Name , Address(has subnodes) and
// PhoneNumber Node , which is subnode of Address :
int curItemIndex = 1; // Each Node Index
// Listing is a Node #0 , Name is #1 , Address #2 , PhoneNumber #3


System.out.println("********");

// First Item is a Name (Internal Index is 1) :
System.out.print(" The Subnode name is "+
dataList.item(curItemIndex).getNodeName());
System.out.println("\t the Value is "+
((Text)(dataList.item(curItemIndex).getFirstChild())).getNodeValue());

System.out.println("********");

Node addressNode = dataList.item(++curItemIndex); //***** For Address Node
if (addressNode.hasChildNodes())
{
System.out.println("has"); // NODE ADDRES HAS NO CHILD NODES
// HERE WHY ???
} // ADDRESS ALREADY HAS IT'S OWN DATA
// AND PhoneNumber NODE , WHICH ALSO
// HAS CHILDS !!!

// The Second Node is Address <------ !!!
System.out.print(dataList.item(curItemIndex).getNodeName());
// It prints here #text ???

// FROM HERE IT TOTALY DOES'NT WORK BECAUSE
// THE API or I AM ?:-) CAN NOT DETERMINATE AN ADDRESS NODE AS A Father NODE !
//**************** New List Especialy FOR Address Data *********************************

NodeList addressDataList = dataList.item(curItemIndex).getChildNodes();
NodeList tmpList = null ; // temporary Node List
int length = addressDataList.getLength();
boolean PhoneNumber = false;
for (int j=0;
j < length;
j++)
{

if (!addressDataList.item(j).hasChildNodes() && !PhoneNumber)
{

System.out.print(" The Subnode name is "+
addressDataList.item(j).getNodeName()+
System.out.println("\t the Value is "+
((Text)( addressDataList.item(j).getFirstChild())).getNodeValue());

// This code should print The next mapped data :
// <StreetNum>77</StreetNum>
// <StreetName>5th Ave</StreetName>
// <City>NY</City>
//<State>NY State</State>
//<Zip>12345</Zip>
}
else
{
if (!PhoneNumber)
{
tmpList = addressDataList.
item(j).getChildNodes();
// Should be with Length of 3

// get Here PhoneNumber Subnodes List
// for First Time
PhoneNumber = true;
j=0;
length = tmpList.getLength();

}


System.out.print(" The Subnode name is "+
(tmpList.item(j).getNodeName());

System.out.println("\t the Value is "+
((Text)( tmpList.item(j).getFirstChild())).getNodeValue());

// Should print here Data for :

// <AreaCode>123</AreaCode>
//<Exchange>exchange</Exchange>
//<Number>12345678</Number>
}
} // for
}
} catch (FactoryConfigurationError e) {
System.out.println("Could not locate a JAXP factory class");
} catch (ParserConfigurationException e) {
System.out.println("Could not locate a JAXP DocumentBuilder class");
} catch (DOMException e) {
System.err.println(e);
} catch(SAXException e)
{
System.err.println(e);
}

catch (IOException e) {
System.err.println(e);
}

I will very appretiate your attempt to help me .

Thank you ,

Marat
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following:

Shows that you have been caught by the very common problem of extraneous text nodes - the parser is turning each crlf appearing between your elements into a text node.
The solution is to NOT depend on the exact index of nodes in the nodelist. Instead, iterated through the nodes and determine what the kind of each one is.
Bill
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
General Comments on getting data out of XML with Java:
Be sure you have the JavaDocs for the org.w3c.dom and related packages - spend some time learning your way around them.
Get very familiar with the methods for the Node and Element classes - especially Node.
There is a very important table in the Node JavaDocs that shows the values returned by getNodeName(), getNodeValue() and getAttributes() for various kinds of Nodes.
Be prepared to write code that looks inefficient - just grit your teeth and do it. For example, in the XML shown in the first post above, if you have a <listing> element you can use something like:

to get child elements of the <AreaCode> type. To get the attached value you have to realize that "123" is a Text Node that is a child of the <AreaCode> element.
Yes, it is clumsy - thats why JDOM is so popular.
Bill
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic