使用Java解析带有4级XML文件的XML文件

时间:2013-08-15 20:59:13

标签: xml parsing

我正在尝试解析此文件和不同级别以获取完全拥有我的数据。请帮忙。我可以解析一个级别的文件但是这个文件正在扼杀我。当我终于认为我得到它然后我进一步搞砸了。我需要遍历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);

1 个答案:

答案 0 :(得分:1)

<强>前言

我在有关java的更新之前发布了此消息。我将把它留下来,因为此解决方案的xpath部分应该可以转移到基于java的解决方案。


以下是使用中执行此操作的方法:

from lxml import etree
doc = etree.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>""")

nsmap = dict(
   s = "http://schemas.xmlsoap.org/soap/envelope/",
   t = "http://tempuri.org/",
   a = "http://schemas.datacontract.org/2004/07/",
   i = "http://www.w3.org/2001/XMLSchema-instance",
)

def name_from_stupid_tag(tag):
   return tag.rpartition('_x003C_')[2].partition('_x003E_')[0]

user_models = []
for user_model in doc.xpath('s:Body/t:GetUsersResponse/t:GetUsersResult/a:UserModel',namespaces=nsmap):
   data = dict((name_from_stupid_tag(e.tag),unicode(e.text) if e.text is not None else e.text) for e in user_model)
   user_models.append(data)

产量:

[{'CanManageUsers': u'1',
  'ConfigurationProfileName': u'Default',
  'DefaultLandingPage': u'1',
  'Email': u'admin@admin.com',
  'Fax': u'555-555-5555',
  'FirstName': u'OnBehalf',
  'LastName': u'Admin',
  'LocaleId': u'1033',
  'Mobile': None,
  'MobileConfigurationProfile': u'Default',
  'OrderPrivilege': u'0',
  'Phone': None,
  'PricingDisplay': None,
  'UserId': u'admin'},
 {'CanManageUsers': u'1',
  'ConfigurationProfileName': u'Default',
  'DefaultLandingPage': u'1',
  'Email': None,
  'Fax': None,
  'FirstName': u'Direct',
  'LastName': u'Sales',
  'LocaleId': u'1033',
  'Mobile': None,
  'MobileConfigurationProfile': u'Default',
  'OrderPrivilege': u'2',
  'Phone': None,
  'PricingDisplay': None,
  'UserId': u'Direct'}]