因此,我有一个表,其中包含以XML存储的大量数据。
部分XML模式(直到我需要的地方)看起来像这样:
<DecisionData>
<Customer>
<SalesAttemptNumber />
<SubLenderID>IN101_CNAC</SubLenderID>
<DecisionType>Decision</DecisionType>
<DealerID />
<CustomerNumber>468195994772076</CustomerNumber>
<CustomerId />
<ApplicationType>Personal</ApplicationType>
<ApplicationDate>9/16/2008 11:32:07 AM</ApplicationDate>
<Applicants>
<Applicant PersonType="Applicant">
<CustNum />
<CustomerSSN>999999999</CustomerSSN>
<CustLastName>BRAND</CustLastName>
<CustFirstName>ELIZABETH</CustFirstName>
<CustMiddleName />
<NumberOfDependants>0</NumberOfDependants>
<MaritalStatus>Single</MaritalStatus>
<DateOfBirth>1/1/1911</DateOfBirth>
<MilitaryRank />
<CurrentAddress>
<ZipCode>46617</ZipCode>
不幸的是,我不熟悉从XML提取信息,而我的google-fu让我失败了。
select TransformedXML.value('(/DecisionData/Customer/Applicants/Applicant PersonType="Applicant"/CurrentAddress/ZipCode/node())[1]','nvarchar(max)') as zip
from XmlDecisionInputText as t
我相信我的问题出在应征者PersonType =“ Applicant”的那一部分,但不确定如何处理。
感谢您的帮助。
答案 0 :(得分:0)
如果有多个申请人,则可以使用CROSS APPLY
示例
Select A.ID
,B.*
From XmlDecisionInputText A
Cross Apply (
Select PersonType = x.v.value('@PersonType','VARCHAR(150)')
,CustLastName = x.v.value('CustLastName[1]','VARCHAR(150)')
,CustFirstName = x.v.value('CustFirstName[1]','VARCHAR(150)')
,ZipCode = x.v.value('CurrentAddress[1]/ZipCode[1]','VARCHAR(150)')
From XmlDecisionInputText.nodes('DecisionData/Customer/Applicants/*') x(v)
) B
答案 1 :(得分:0)
最简单形式的xpath是:
void invokeMethod(String method, String className) {
String mType = getPackageName() + "." + className;
try {
Class mClass = Class.forName(mType);
Constructor<?> cons = mClass.getConstructor(Context.class);
Object o = cons.newInstance(getApplicationContext());
Method mMethod = mClass.getMethod(method);
mMethod.invoke(o);
} catch (Exception e) {
e.printStackTrace();
}
}
这将在文档内的任何位置找到第一个ZipCode节点。如果有多个,则要具体(随您的便,但不要更多):
TransformedXML.value('(//ZipCode)[1]', 'nvarchar(100)') AS zip