• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

xml parsing

when i use the below java code for the atttached xml i got all
null values..

where did i made a mistake..
testingxml.java
f2dEtlConfigExample.xml
0
shragi
Asked:
shragi
  • 4
1 Solution
 
for_yanCommented:
But none off your tags in the code seem to correspond to tags in the
file
0
 
for_yanCommented:
if (qName.toUpperCase().equalsIgnoreCase("f2dconnection_username")) {
						f2dconnection_username = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("f2dconnection_password")) {
						f2dconnection_password = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("f2ddocbase")) {
						f2ddocbase = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("f2d_filesystem_location")) {
						f2d_filesystem_location = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("f2d_destination_location")) {

Open in new window


There are no f2dconnection_password, f2ddocbase, etc.
They need to match - so it would not recognize
When they match there could be other problems,
but if they don't it would not find them for sure

  <?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
- <config>
- <!--  Source 
  --> 
- <extract type="filesystem">
- <!--  filesystem xml definition below 
  --> 
- <connection_info>
  <root>C:\Temp</root> 
  </connection_info>
  </extract>
- <!--  Target 
  --> 
- <load type="docbase">
- <!--  docbase xml definition below 
  --> 
- <connection_info>
  <username>dmadmin</username> 
  <password>password1!</password> 
  <docbase>myNewDocbase</docbase> 
  </connection_info>
  <destination>/Temp Cabinet</destination> 
  </load>
  </config>

Open in new window


0
 
for_yanCommented:


This is the output which I got after some obvious modifications
in the code to match the elements
(in the input file in the sceond line

-<config>

needs to be replced

by

<config>
(without first dash);


package forXML;


import java.io.File;


import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class mysaxparser {
	//static File scpath = new File("C:/sampleF2DEtlConfig.xml");
	static String filename = "sampleF2DEtlConfig.xml";
	static String[] oldXMLValues = new String[6];
	public static void main( String args[])
	{
		System.out.println("hi");

		mysaxparser.getElementFromXML();
		for(int i=0;i<6;i++)
		{
			System.out.println(oldXMLValues[i]);
		}
	}
	public static  void getElementFromXML() {
		//final InputFormVO inputFormXML=new InputFormVO();
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			DefaultHandler handler = new DefaultHandler() {
				boolean f2dconnection_username=false;
				boolean f2dconnection_password =false;
				boolean f2ddocbase =false;
				boolean f2d_filesystem_location=false;
				boolean f2d_destination_location=false;
				boolean extract =false;
				boolean load = false;
				boolean connection_info =false;
				public void startElement(String uri, String localName,
						String qName, Attributes attributes)
						throws SAXException {
					if (qName.toUpperCase().equalsIgnoreCase("username")) {
						f2dconnection_username = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("password")) {
						f2dconnection_password = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("docbase")) {
						f2ddocbase = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("f2d_filesystem_location")) {
						f2d_filesystem_location = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("destination")) {
						f2d_destination_location = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("connection_info")) {
						connection_info = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("extract")) {
						extract = true;
					}
					if (qName.toUpperCase().equalsIgnoreCase("load")) {
						load = true;
					}
				}
				public void endElement(String uri, String localName,
						String qName) throws SAXException {
					if (qName.toUpperCase().equalsIgnoreCase("connection_info")) {
						connection_info = false;
                     }

                	if (qName.toUpperCase().equalsIgnoreCase("extract")) {
                		extract = false;

                    }
                	if (qName.toUpperCase().equalsIgnoreCase("load")) {
						load = false;

                    }

				}
				public void characters(char ch[], int start, int length)
						throws SAXException {
					if (extract && connection_info && f2d_filesystem_location) {
						oldXMLValues[0] = new String(ch, start, length);
						f2d_filesystem_location = false;
						//System.out.println(oldXMLValues[0]);

					}
					if (load && connection_info && f2dconnection_username) {
						oldXMLValues[1] =new String(ch, start, length);
						f2dconnection_username = false;
						//System.out.println(oldXMLValues[1]);
					}
					if (load && connection_info && f2dconnection_password) {
						oldXMLValues[2] = new String(ch, start, length);
						f2dconnection_password = false;
						//System.out.println(oldXMLValues[2]);
					}
					if (load && connection_info && f2ddocbase) {
						oldXMLValues[3] = new String(ch, start, length);
						f2ddocbase = false;
						//System.out.println(oldXMLValues[3]);
					}
					if (load && f2d_destination_location) {
						oldXMLValues[4] = new String(ch, start, length);
						f2d_destination_location = false;
						//System.out.println(oldXMLValues[4]);
					}
					}
			};
			saxParser.parse(filename, handler);

		}
		catch (Exception e) {
			e.printStackTrace();
		}

	}

}

Open in new window


Output:

hi
null
dmadmin
password1!
myNewDocbase
/Temp Cabinet
null

Open in new window

0
 
objectsCommented:
lot simpler to just use xpath
0
 
for_yanCommented:

This is with XPath:

package forXML;


import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import java.io.IOException;


public class WithXPath {

public static void main(String[] args)
   throws ParserConfigurationException, SAXException,
        IOException, XPathExpressionException {

    DocumentBuilderFactory domFactory =
    DocumentBuilderFactory.newInstance();
          domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("sampleF2DEtlConfig.xml");
    XPath xpath = XPathFactory.newInstance().newXPath();
       // XPath Query for showing all nodes value
    XPathExpression expr = xpath.compile("//config/*/*/*/text()");

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
     System.out.println(nodes.item(i).getNodeValue());
    }
  }


}

Open in new window


Output:

C:\Temp
dmadmin
password1!
myNewDocbase

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now