本文介绍了如何从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>*&D1</sactcd>
<sactnm>*JAGDISH & CO</sactnm>
<address>-</address>
<place>D-21 APMC</place>
<place1>D-21 APMC</place1>
<phone />
<email />
</FFSM>
</NewDataSet>
推荐答案
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 String解析和Retrive列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!