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