如何从xml String解析和Retrive列名 [英] How to parse and Retrive column names from xml String

查看:88
本文介绍了如何从xml String解析和Retrive列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何解析以下字符串以获取Java中的所有列名?

Hi All,
How to Parse following string to get All Column Names in Java?

<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>

推荐答案

试试这个

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 "";
	 }


你正在把它解析为XML,这很好(你没解释你的问题是什么)。



但这不仅仅是XML,而是代表 XML模式的专用XML,因此最好使用专门的XSD解析器。请参阅:

https:// xsom .java.net / nonav / javadoc / com / sun / xml / xsom / parser / XSOMParser.html [ ^ ],
来自 .net> https://xsom.java.net [ ^ ]。



-SA
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


这篇关于如何从xml String解析和Retrive列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆