我正在尝试解析此文件和不同级别以获取完全拥有我的数据。请帮忙。我可以解析一个级别的文件,但这个文件正在杀死我。当我最终认为我得到了它时,我把它弄得更糟了。我需要遍历 UserModel 记录。我需要解析它的编程语言是 Java。下面我提供两个示例。XML 文件和我正在编写的代码。我认为我没有正确地深入到我想要循环的元素。在此示例中,我只能获取记录的一个实例并且循环停止。所以很明显有些不对劲。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetUsersResponse xmlns="http://tempuri.org/">
<GetUsersResult xmlns:a="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:UserModel>
<a:_x003C_CanManageUsers_x003E_k__BackingField>1</a:_x003C_CanManageUsers_x003E_k__BackingField>
<a:_x003C_ConfigurationProfileName_x003E_k__BackingField>Default</a:_x003C_ConfigurationProfileName_x003E_k__BackingField>
<a:_x003C_DefaultLandingPage_x003E_k__BackingField>1</a:_x003C_DefaultLandingPage_x003E_k__BackingField>
<a:_x003C_Email_x003E_k__BackingField>admin@admin.com</a:_x003C_Email_x003E_k__BackingField>
<a:_x003C_Fax_x003E_k__BackingField>555-555-5555</a:_x003C_Fax_x003E_k__BackingField>
<a:_x003C_FirstName_x003E_k__BackingField>OnBehalf</a:_x003C_FirstName_x003E_k__BackingField>
<a:_x003C_LastName_x003E_k__BackingField>Admin</a:_x003C_LastName_x003E_k__BackingField>
<a:_x003C_LocaleId_x003E_k__BackingField>1033</a:_x003C_LocaleId_x003E_k__BackingField>
<a:_x003C_MobileConfigurationProfile_x003E_k__BackingField>Default</a:_x003C_MobileConfigurationProfile_x003E_k__BackingField>
<a:_x003C_Mobile_x003E_k__BackingField/>
<a:_x003C_OrderPrivilege_x003E_k__BackingField>0</a:_x003C_OrderPrivilege_x003E_k__BackingField>
<a:_x003C_Phone_x003E_k__BackingField/>
<a:_x003C_PricingDisplay_x003E_k__BackingField/>
<a:_x003C_UserId_x003E_k__BackingField>admin</a:_x003C_UserId_x003E_k__BackingField>
</a:UserModel>
<a:UserModel>
<a:_x003C_CanManageUsers_x003E_k__BackingField>1</a:_x003C_CanManageUsers_x003E_k__BackingField>
<a:_x003C_ConfigurationProfileName_x003E_k__BackingField>Default</a:_x003C_ConfigurationProfileName_x003E_k__BackingField>
<a:_x003C_DefaultLandingPage_x003E_k__BackingField>1</a:_x003C_DefaultLandingPage_x003E_k__BackingField>
<a:_x003C_Email_x003E_k__BackingField/>
<a:_x003C_Fax_x003E_k__BackingField/>
<a:_x003C_FirstName_x003E_k__BackingField>Direct</a:_x003C_FirstName_x003E_k__BackingField>
<a:_x003C_LastName_x003E_k__BackingField>Sales</a:_x003C_LastName_x003E_k__BackingField>
<a:_x003C_LocaleId_x003E_k__BackingField>1033</a:_x003C_LocaleId_x003E_k__BackingField>
<a:_x003C_MobileConfigurationProfile_x003E_k__BackingField>Default</a:_x003C_MobileConfigurationProfile_x003E_k__BackingField>
<a:_x003C_Mobile_x003E_k__BackingField/>
<a:_x003C_OrderPrivilege_x003E_k__BackingField>2</a:_x003C_OrderPrivilege_x003E_k__BackingField>
<a:_x003C_Phone_x003E_k__BackingField/>
<a:_x003C_PricingDisplay_x003E_k__BackingField/>
<a:_x003C_UserId_x003E_k__BackingField>Direct</a:_x003C_UserId_x003E_k__BackingField>
</a:UserModel>
</GetUsersResult>
</GetUsersResponse>
</s:Body>
</s:Envelope>
现在这是我如何开始执行此操作的 Java 代码:
File fXmlFile = new File("C:\\Users\\User\\Documents\\testUsers.xml");
Document dom = (Document) db.parse(fXmlFile);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getChildNodes();
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
// System.out.println("Node type is: "+nl.item(i).getNodeType());
// System.out.println("Element Node is: "+Node.ELEMENT_NODE);
if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) nl.item(i);
// System.out.println("Node Name: "+el.getNodeName());
//System.out.println(el.getElementsByTagName("a:UserModel").item(0).getTextContent());
if (el.getNodeName().contains("a:UserModel")) {
//System.out.println("The loop cycle is: "+i);
String canManageUsers = el.getElementsByTagName("a:_x003C_CanManageUsers_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Can Manage Users", canManageUsers);
//System.out.println(canManageUsers);
String confProfileName = el.getElementsByTagName("a:_x003C_ConfigurationProfileName_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Configuration Profile Name", confProfileName);
// System.out.println(confProfileName);
String defaultLandingPage = el.getElementsByTagName("a:_x003C_DefaultLandingPage_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Default Landing Page", defaultLandingPage);
//System.out.println(defaultLandingPage);
String email = el.getElementsByTagName("a:_x003C_Email_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("E-Mail", email);
//System.out.println(email);
String fax = el.getElementsByTagName("a:_x003C_Fax_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Fax", fax);
//System.out.println(fax);
String fname = el.getElementsByTagName("a:_x003C_FirstName_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("First Name", fname);
//System.out.println(fname);
String lname = el.getElementsByTagName("a:_x003C_LastName_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Last Name", lname);
//System.out.println(lname);
String localeId = el.getElementsByTagName("a:_x003C_LocaleId_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Locale ID", localeId);
//System.out.println(localeId);
String mobileConfProfile = el.getElementsByTagName("a:_x003C_MobileConfigurationProfile_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Mobile Configuration Profile", mobileConfProfile);
//System.out.println(mobileConfProfile);
String mobile = el.getElementsByTagName("a:_x003C_Mobile_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Mobile", mobile);
//System.out.println(mobile);
String orderPrivilege = el.getElementsByTagName("a:_x003C_OrderPrivilege_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Can Submit Orders", orderPrivilege);
//System.out.println(orderPrivilege);
String phone = el.getElementsByTagName("a:_x003C_Phone_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Phone", phone);
//System.out.println(phone);
String pricingDisplay = el.getElementsByTagName("a:_x003C_PricingDisplay_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("Pricing Display Mode", pricingDisplay);
//System.out.println(pricingDisplay);
String userId = el.getElementsByTagName("a:_x003C_UserId_x003E_k__BackingField").item(0).getTextContent();
dataMap.put("User ID", userId);
//System.out.println(userId);