0

親ノードの下に 2 つのノードが存在する場合、ノードの値を取得するにはどうすればよいですか。例: 次の Xml があります。

<?xml version="1.0" encoding="UTF-8"?>
<Services xmlns="http://sample.schema.com/abc">
    <service>
        <name>Sample</name>
        <uri>/v9.0/sample.123.com
        </uri>
    </service>
    <service>
        <name>Sample 2</name>
    </service>
    <service>
        <name>Sample 3</name>
        <uri>/v9.0/sample3.123.com
        </uri>
    </service>
    <service>
        <name>Sample 4</name>
        <uri>/v9.0/sample4.123.com
        </uri>
    </service>
    <service>
        <name>Sample 5</name>
        <uri>/v9.0/sample5.123.com
        </uri>
    </service>
    <service>
        <name>Sample 6</name>

    </service>
    <service>
        <name>Sample 7</name>
        <uri>/v9.0/sample7.123.com
            </uri>
    </service>
    <service>
        <name>Sample 8</name>
    </service>
</Services>

私のコード:

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class SimpleXpath {
    public static void main(String[] args) throws ParserConfigurationException,
            SAXException, IOException, XPathExpressionException {
          DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(false); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse("testService1.xml");
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile("//Services/service[(name) and (uri)/text()]");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
           for (int i = 0; i < nodes.getLength(); i++) {
            String value=nodes.item(i).getNodeValue();
               System.out.println(" output : "+i+"  "+value); 
            }
    }

}

name と uri が service ノードの下にある場合、上記の xml から name と url の値を読み取りたい。一部のサービス ノードには名前のみが含まれていることがわかります。そっちは避けたい。私の xpath 式は出力として null 値を与えます。

サービスに両方が含まれている場合、名前と uri の「テキスト」を取得するにはどうすればよいですか?

xpath を使用して、名前を最初に、uri を 2 番目として出力を取得できますか (両方がサービスの下にある場合)。

どうもありがとう。

ジョン

4

4 に答える 4

0

まず、より単純な xpath: を使用できます//Services/service[name and uri]service次に、NodeList は要素のリストを返します。name他のその子を繰り返し、 andという名前の要素を見つけてuri、それらのテキスト値を取得できます。または、次のようにさらに 2 つの xpath 式を作成することもできます。

        XPathExpression exprName = xpath.compile("normalize-space(name)");
        String name = (String)exprName.evaluate(serviceNode, XPathConstants.STRING);

uri も同様です。

于 2013-04-22T10:08:25.740 に答える
0

ありがとうございました....次のコードは要件を満たしています。品質を改善する他のオプションがあれば提案してください。

public static void main(String[] args) throws ParserConfigurationException,
            SAXException, IOException, XPathExpressionException {
          DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(false); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse("testService1.xml");
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile("//Services/service[(name) and (uri)]/name/text()");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;

            XPathExpression expr1 = xpath.compile("//Services/service[(name) and (uri)]/uri/text()");
            Object result1 = expr1.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes1 = (NodeList) result1;

           for (int i = 0; i < nodes1.getLength()&&i < nodes.getLength(); i++) {
               String value=nodes.item(i).getNodeValue();
            String value1=nodes1.item(i).getNodeValue();
               System.out.println(" output : "+i+"  "+value+ " "+ value1); 
            }
    }
于 2013-04-22T10:17:28.090 に答える
0

2 つの子を持つこの xpath select 要素

//*[count(*)=2]
于 2013-04-25T21:55:17.090 に答える