Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
Hi All,
How to Parse following string to get All Column Names in Java?
XML
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="FFSM" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="FFSM">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="gactcd" type="xs:string" minOccurs="0" />
                <xs:element name="sactcd" type="xs:string" minOccurs="0" />
                <xs:element name="sactnm" type="xs:string" minOccurs="0" />
                <xs:element name="address" type="xs:string" minOccurs="0" />
                <xs:element name="place" type="xs:string" minOccurs="0" />
                <xs:element name="place1" type="xs:string" minOccurs="0" />
                <xs:element name="phone" type="xs:string" minOccurs="0" />
                <xs:element name="email" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <FFSM>
      <gactcd>D</gactcd>
    <sactcd>*&amp;D1</sactcd>
    <sactnm>*JAGDISH &amp; CO</sactnm>
    <address>-</address>
    <place>D-21 APMC</place>
    <place1>D-21 APMC</place1>
    <phone />
    <email />
  </FFSM>
  </NewDataSet>
Posted
Updated 23-Jul-15 21:36pm
v2
Comments
Tomas Takac 6-May-15 9:22am    
What have you tried?
Shruti91 6-May-15 9:33am    
I Wants to use in This Way
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));

Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("FFSM");

for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);

NodeList name = element.getElementsByTagName("gactcd");
Element line = (Element) name.item(0);
System.out.println("General AC Code: " + getCharacterDataFromElement(line));

}

try this
public static Document getDomElement(String xml){
		Document doc = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {

			DocumentBuilder db = dbf.newDocumentBuilder();

			InputSource is = new InputSource();
		        is.setCharacterStream(new StringReader(xml));
		        doc = db.parse(is); 
		       

			} catch (ParserConfigurationException e) {
				
				System.out.println("Error: "+ e.getMessage());
				return null;
			} catch (SAXException e) {
				System.out.println("Error: "+ e.getMessage());
	            return null;
			} catch (IOException e) {
				System.out.println("Error: "+ e.getMessage());
				return null;
			}
	        return doc;
	}
	public final static String getElementValue( Node elem ) {
	     Node child;
	     if( elem != null){
	         if (elem.hasChildNodes()){
	             for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
	                 if( child.getNodeType() == Node.TEXT_NODE  ){
	                     return child.getNodeValue();
	                 }
	             }
	         }
	     }
	     return "";
	 }
 
Share this answer
 
Comments
Shruti91 18-May-15 3:09am    
But How to use this to do my task?
Sid_Joshi 18-May-15 3:17am    
Document DOC=getDomElement(line);
Node n = DOC.getFirstChild();
NodeList nl = n.getChildNodes();
Node row,col;

for (int i=1; i < nl.getLength(); i++) {
row = nl.item(i);
// System.out.println(row.getNodeName());
NodeList list=row.getChildNodes();
for (int j=0; j < list.getLength(); j++){
col = list.item(j);
System.out.println(col.getNodeName()+"="+getElementValue(col));

}
System.out.println("-----------------------------------------------------");
}
Shruti91 18-May-15 3:43am    
Thank you....
You are parsing it as XML, which is perfectly fine (you did not explain what your problem is).

But this is not just XML, but specialized XML representing XML schema, so it would be better to use specialized parser for XSD. Please see:
https://xsom.java.net/nonav/javadoc/com/sun/xml/xsom/parser/XSOMParser.html[^],
from https://xsom.java.net[^].

—SA
 
Share this answer
 
Comments
Shruti91 7-May-15 2:42am    
your links doesn't get me solution.
I created DataTable class in java which is similar like System.Data.DataTable
and I wants to load records from xml string to my DataTable.
Sergey Alexandrovich Kryukov 7-May-15 9:20am    
It will give you more than your own code — the XML schema. This is exactly what you need to create database schema — tables and all.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900