如何在java中获取复杂嵌套xml的属性名称,值及其标记名称和值

时间:2017-04-30 12:20:40

标签: java xml dom

您好我有一个复杂的大型嵌套xml,我必须解析并插入数据库。 我使用JAVA X路径完成了它,但速度非常慢。

是否有任何方法可以获取所有属性名称和值,还可以标记以下xml的名称和值。 这是我的xml。 只需添加更多信息contentSet重复,儿童代码MentalSeriesStatements也会重复。

<evn:Body mVer="0.0" mjVe="1" contentSet="Mental">
<evn:Operation action="Edit">
<evn:Data si:type="al:MentalPeriodDataItem">

<al:MentalSeriesPeriod mentalSeriesPeriodIndex="33" mentalSeriesPeriodType="HalfYear" mentalSeriesPeriodEndDate="20000930T00:00:00+00:00">

<al:MentalSeriesId objectType="MentalSeries" objectTypeId="7428374">23984623874</al:MentalSeriesId>

<al:UniqueMentalSet objectType="Mental" objectTypeId="404084">48433426923</al:UniqueMentalSet>

<al:MentalSeriesStatements>


<al:MentalSeriesStatement effectiveTo="20000930T00:00:00+00:00" effectiveFrom="20000330T00:00:00+00:00">
<al:MentalSeriesStatementTypeCode>BAL</al:MentalSeriesStatementTypeCode>
<al:MentalSeriesStatementPeriodId>137</al:MentalSeriesStatementPeriodId>
<al:MentalSeriesStatementSourceId>55</al:MentalSeriesStatementSourceId>
<al:MentalSeriesStatementCurrencyId objectType="Currency" objectTypeId="404014">500186</al:MentalSeriesStatementCurrencyId>
<al:IsUsedForAnalytics>true</al:IsUsedForAnalytics>
</al:MentalSeriesStatement>

<al:MentalSeriesStatement effectiveTo="20000930T00:00:00+00:00" effectiveFrom="20000330T00:00:00+00:00">
<al:MentalSeriesStatementTypeCode>CAS</al:MentalSeriesStatementTypeCode>
<al:MentalSeriesStatementPeriodId>39</al:MentalSeriesStatementPeriodId>
<al:MentalSeriesStatementSourceId>55</al:MentalSeriesStatementSourceId>
<al:MentalSeriesStatementCurrencyId objectType="Currency" objectTypeId="404014">500186</al:MentalSeriesStatementCurrencyId>
<al:IsUsedForAnalytics>true</al:IsUsedForAnalytics>
</al:MentalSeriesStatement>

<al:MentalSeriesStatement effectiveTo="20000930T00:00:00+00:00" effectiveFrom="20000330T00:00:00+00:00">
<al:MentalSeriesStatementTypeCode>FTN</al:MentalSeriesStatementTypeCode>
<al:MentalSeriesStatementPeriodId>39</al:MentalSeriesStatementPeriodId>
<al:MentalSeriesStatementSourceId>55</al:MentalSeriesStatementSourceId>
<al:MentalSeriesStatementCurrencyId objectType="Currency" objectTypeId="404014">500186</al:MentalSeriesStatementCurrencyId>
<al:IsUsedForAnalytics>true</al:IsUsedForAnalytics>
</al:MentalSeriesStatement>

<al:MentalSeriesStatement effectiveTo="20000930T00:00:00+00:00" effectiveFrom="20000330T00:00:00+00:00">
<al:MentalSeriesStatementTypeCode>INC</al:MentalSeriesStatementTypeCode>
<al:MentalSeriesStatementPeriodId>39</al:MentalSeriesStatementPeriodId>
<al:MentalSeriesStatementSourceId>55</al:MentalSeriesStatementSourceId>
<al:MentalSeriesStatementCurrencyId objectType="Currency" objectTypeId="404014">500186</al:MentalSeriesStatementCurrencyId>
<al:IsUsedForAnalytics>true</al:IsUsedForAnalytics>
</al:MentalSeriesStatement>

<al:MentalSeriesStatement effectiveTo="20000930T00:00:00+00:00" effectiveFrom="20000330T00:00:00+00:00">
<al:MentalSeriesStatementTypeCode>SHE</al:MentalSeriesStatementTypeCode>
<al:MentalSeriesStatementPeriodId>39</al:MentalSeriesStatementPeriodId>
<al:MentalSeriesStatementSourceId>55</al:MentalSeriesStatementSourceId>
<al:MentalSeriesStatementCurrencyId objectType="Currency" objectTypeId="404014">500186</al:MentalSeriesStatementCurrencyId>
<al:IsUsedForAnalytics>true</al:IsUsedForAnalytics>
</al:MentalSeriesStatement>

</al:MentalSeriesStatements>

</al:MentalSeriesPeriod>
</evn:Data>

这是我的示例代码

String SeriesPeriodExpression ="/path[@action='"+strAction+"']/Data[@type='"+DataType+"']/mentalSeriesPeriod";
SeriesPeriodExpressionList = (NodeList) xPath.compile(mentalSeriesPeriodExpression).evaluate(xmlDocument, XPathConstants.NODESET);

 String SeriesIdExpr =SeriesPeriodExpression+"/SeriesId";
 NodeList SeriesIdList = (NodeList) xPath.compile(SeriesIdExpr).evaluate(xmlDocument, XPathConstants.NODESET);

 String SeriesId_objectTypeIdExpr =SeriesPeriodExpression+"/SeriesId/@objectTypeId";
 NodeList SeriesId_objectTypeIdExprList = (NodeList) xPath.compile(SeriesId_objectTypeIdExpr).evaluate(xmlDocument, XPathConstants.NODESET);

 String SeriesId_objectTypeExpr =SeriesPeriodExpression+"/SeriesId/@objectType";
 NodeList SeriesId_objectTypeList = (NodeList) xPath.compile(SeriesId_objectTypeExpr).evaluate(xmlDocument, XPathConstants.NODESET);

 for (int i = 0; i < SeriesPeriodExpressionList.getLength(); i++) {
     String SeriesId = "";
     String SeriesId_objectTypeId ="";
     String SeriesId_objectType ="";

     if(SeriesIdList.getLength()==SeriesPeriodExpressionList.getLength()){
         SeriesId = SeriesIdList.item(i).getFirstChild().getNodeValue();
         sbSeriesPeriod.append(SeriesIdList.item(i).getFirstChild().getNodeValue() + "~");
     }

     if(SeriesId_objectTypeIdExprList.getLength()==SeriesPeriodExpressionList.getLength()){
         SeriesId_objectTypeId = SeriesId_objectTypeIdExprList.item(i).getFirstChild().getNodeValue();
         sbSeriesPeriod.append(SeriesId_objectTypeId + "~");
     }

     if(SeriesId_objectTypeList.getLength()==SeriesPeriodExpressionList.getLength()){
          SeriesId_objectType = SeriesId_objectTypeList.item(i).getFirstChild().getNodeValue();
         sbSeriesPeriod.append(SeriesId_objectType + "~");
     }


     String SeriesStatementsExpression ="/ContentEnvelope/Body/ContentItem[@action='"+strAction+"']/Data[@type='"+DataType+"']/SeriesPeriod[@fundamentalSeriesPeriodIndex='"+SeriesPeriod_fundamentalSeriesPeriodIndex+"'][@fundamentalSeriesPeriodEndDate='"+SeriesPeriod_fundamentalSeriesPeriodEndDate+"'][./SeriesId ='"+SeriesId+"']/SeriesStatements/SeriesStatement";
     NodeList SeriesStatementsList = (NodeList) xPath.compile(SeriesStatementsExpression).evaluate(xmlDocument, XPathConstants.NODESET);
     System.out.println(SeriesStatementsList.getLength());
     String SeriesStatementTypeCodeExpr =SeriesStatementsExpression+"/SeriesStatementTypeCode";
     NodeList SeriesStatementTypeCodeList = (NodeList) xPath.compile(SeriesStatementTypeCodeExpr).evaluate(xmlDocument, XPathConstants.NODESET);

     String SeriesStatementPeriodIdExpr =SeriesStatementsExpression+"/SeriesStatementPeriodId";
     NodeList SeriesStatementPeriodIdList = (NodeList) xPath.compile(SeriesStatementPeriodIdExpr).evaluate(xmlDocument, XPathConstants.NODESET);

     String SeriesStatementSourceIdExpr =SeriesStatementsExpression+"/SeriesStatementSourceId";
     NodeList SeriesStatementSourceIdList = (NodeList) xPath.compile(SeriesStatementSourceIdExpr).evaluate(xmlDocument, XPathConstants.NODESET);

     for (int k = 0; k < SeriesStatementsList.getLength(); k++) {
        String SeriesStatementTypeCode ="";
        String SeriesStatementPeriodId = "";
        String SeriesStatementSourceId = "";

            if(UniqueSet_objectTypeIdList.getLength()==SeriesStatementsList.getLength()){
            SeriesStatementTypeCode = SeriesStatementTypeCodeList.item(k).getFirstChild().getNodeValue();
        }
        if(SeriesStatementPeriodIdList.getLength()==SeriesStatementsList.getLength()){
            SeriesStatementPeriodId = SeriesStatementPeriodIdList.item(k).getFirstChild().getNodeValue(); 
        }
        if(SeriesStatementSourceIdList.getLength()==SeriesStatementsList.getLength()){
            SeriesStatementSourceId = SeriesStatementSourceIdList.item(k).getFirstChild().getNodeValue(); 
        }

            }

}

0 个答案:

没有答案