简体   繁体   English

在Groovy文件中使用Java获取子内部XML标签值

[英]Fetch sub inner XML Tag values using Java in Groovy file

I'm trying to fetch xml tag values. 我正在尝试获取xml标签值。 Issue here is I have same tag in different inner tags. 这里的问题是我在不同的内部标签中有相同的标签。 I want to fetch the tag values which is under tag. 我想获取标签下的标签值。 Here is the code 这是代码

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <getResponse
        xmlns="http://bankBazaarService.icicibank.com">
        <getResult>
            <arrayOfContext>
                <item>
                    <errorCode></errorCode>
                    <errorDescription></errorDescription>
                </item>
            </arrayOfContext>
            <typeOfEmploymentDetails>
                <type></type>
                <companyName></companyName>
                <typeOfBusiness></typeOfBusiness>
            </typeOfEmploymentDetails>
            <getOffers>
                <item>
                    <offerId>1</offerId>                        
                </item>
                <item>
                    <offerId>2</offerId>
                </item>
            </getOffers>
            </getResult>
        </getResponse>
    </soapenv:Body>
</soapenv:Envelope>

My code is 我的代码是

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;

    builder = factory.newDocumentBuilder();
    Document document = builder.parse( new InputSource( new StringReader( responseXML ) ) );
    NodeList nList = document.getElementsByTagName("item");
    for (int i = 0; i < nList.getLength(); i++) {
        Element element = (Element) nList.item(i);
        println element
        String offerUd = element.getElementsByTagName("offerId").item(0).textContent?:""
        println offerId
    }

Element prints as 元素打印为

<?xml version="1.0" encoding="UTF-8"?><item>
                    <errorCode/>
                    <errorDescription/>
                </item>

I'm actually trying to fetch 1 我实际上是在尝试获取1

So that I can get the values of the tag. 这样我就可以获取标签的值。 ie offerId. 即offerId。 Is there any issue in the code 代码中是否有任何问题

I took your code, fixed the typos ( offerUd --> offerId ), made it into Java: 我拿了您的代码,修正了拼写错误( offerUd > offerId ),并将其放入Java中:

@Test
public void test() throws ParserConfigurationException, IOException, SAXException
{
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder;

  builder = factory.newDocumentBuilder();
  org.w3c.dom.Document document = builder.parse(new InputSource(new StringReader(responseXML)));
  NodeList nList = document.getElementsByTagName("item");
  for (int i = 0; i < nList.getLength(); i++)
  {
    Element element = (Element) nList.item(i);
    println(element.toString());
    String offerUd = element.getElementsByTagName("offerId").item(0).getTextContent() != null ? element.getElementsByTagName("offerId").item(0).getTextContent() : "";
    println(offerUd);
  }
}

private void println(String t)
{
  System.out.println(t);
}

And I got a NullPointerException on getting item 0 of the "offerId" tags, because the first item in your XML does not have an "offerId" tag. 我在获取"offerId"标签的项目0遇到了NullPointerException,因为XML中的第一项没有"offerId"标签。

java.lang.NullPointerException
    at xml.ReadXML.test(ReadXML.java:75)

Update: here's working code. 更新:这是工作代码。 Get the GetOffers element first, then get the items beneath it. 首先获取GetOffers元素,然后获取其下方的项目。

@Test
public void test() throws ParserConfigurationException, IOException, SAXException
{
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder;

  builder = factory.newDocumentBuilder();
  org.w3c.dom.Document document = builder.parse(new InputSource(new StringReader(responseXML)));
  NodeList offersList = document.getElementsByTagName("getOffers");
  for (int j = 0; j < offersList.getLength(); j++)
  {
    Element offer = (Element) offersList.item(j);

    NodeList nList = offer.getElementsByTagName("item");
    for (int i = 0; i < nList.getLength(); i++)
    {
      Element element = (Element) nList.item(i);

      String offerUd = element.getElementsByTagName("offerId").item(0).getTextContent() != null ? element.getElementsByTagName("offerId").item(0).getTextContent() : "";
      System.out.println(offerUd);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM