【问题标题】:Loop through XML String using XPath - Java使用 XPath 循环遍历 XML 字符串 - Java
【发布时间】:2014-02-28 11:59:38
【问题描述】:

我有一个函数我想遍历 xml 并提取某些标签。

我的 xml 看起来像这样:

<Report_Data>
    <Report_Entry>
        <Company>Test</Company>
        <Name>Test Name</Name>
        <Division>Test Division</Division>
    </Report_Entry>
    <Report_Entry>
        <Company>Test 2</Company>
        <Name>Test Name 2</Name>
        <Division>Test Division 2</Division>
    </Report_Entry>
    <Report_Entry>
        <Company>Test 3</Company>
        <Name>Test Name 3</Name>
        <Division>Test Division 3</Division>
    </Report_Entry>
</Report_Data>

这是我要循环的代码:

String comp, name, div, nodeName, NodeValue;
Node node;
try
{
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();

InputSource source = new InputSource(new StringReader(coaFULL));
Document doc2 = (Document) xpath.evaluate("/", source, XPathConstants.NODE);

NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry").evaluate(doc2, XPathConstants.NODESET);
System.out.println("NODE LIST LENGTH =" + nodeList.getLength());

String nodeName, nodeValue = "";
Node node;

for(int i = 0; i < nodeList.getLength(); i++)
{
    node = nodeList.item(i);
    node = nodeList.item(i).getFirstChild();
    nodeName = node.getNodeName();
    nodeValue = node.getChildNodes().item( 0 ).getNodeValue();

    if(nodeName.equals("Company"))
    {
        comp = nodeValue;
    }
    else if( nodeName.equals("Name"))
    {
        name = nodeValue;
    }
    else if(nodeName.equals("Division"))
    {
        div = nodeValue;
    }
    System.out.println("COMPANY = " + comp);
    System.out.println("NAME = " + name);
    System.out.println("DIVISION = " + div);
}

当我运行我的代码时,只有第一个值(公司)得到一个实际值,其他的都是空白的。我还尝试在每个 if 语句中添加 node = nodeList.item(i).getNextSibling(); 以获取下一个节点,但这不起作用。

我的 nodeList 里面确实有项目,超过 1000 个。这句话有问题吗:NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry").evaluate(doc2, XPathConstants.NODESET);?

应该是:NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry/*").evaluate(doc2, XPathConstants.NODESET);

我在最后尝试了/*,但这导致 nodeList 中包含每个节点。我想确保当我抓取一个 Report_Entry 节点时,我将字符串变量设置为彼此对应的正确值。

================================================ ===========

解决方案:这很难看,但我的解决方案是只使用一个循环并使用具有硬编码值的第二个子节点列表:

for(int i = 0; i < nodeList.getLength(); i++)
{
    node = nodeList.item(i);
    tempList = node.getChildNodes();
    System.out.println("TEMP LIST LENGTH =" + tempList.getLength());
    comp = tempList.item(0).getTextContent();
    name = tempList.item(1).getTextContent();
    div = tempList.item(2).getTextContent();
}

感谢@hage 的帮助。

【问题讨论】:

    标签: java xml xpath


    【解决方案1】:

    也许是因为你的node 只是第一个孩子?

    node = nodeList.item(i);
    node = nodeList.item(i).getFirstChild();
    

    我猜nodeList.item(i) 会给你Report_Entrys,他们的第一个孩子是Company。

    您需要遍历Company 条目的所有子项

    编辑(关于您的编辑):

    tempList.item(x) 是 Company、Name,然后是 Division。当你得到这个的第一个孩子时,你在文本节点(实际内容)。因为您尝试获取此节点的名称,所以您会得到#text 输出(see this)。

    要获取节点的名称和值,试试这个(未经测试)

    nodeName = tempList.item(x).getNodeName();
    nodeValue = tempList.item(x).getTextContent();
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 2021-04-02
      • 2014-09-19
      • 2023-03-23
      • 2016-01-24
      相关资源
      最近更新 更多