Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java中的xpath浏览此列表?_Java_Xpath - Fatal编程技术网

使用java中的xpath浏览此列表?

使用java中的xpath浏览此列表?,java,xpath,Java,Xpath,我有一个包含许多不同节点的xml文件。其中一些特别是嵌套的,如下所示: <emailAddresses> <emailAddress> <value>sambj1981@gmail.com</value> <typeSource>WORK</typeSource> <typeUser><

我有一个包含许多不同节点的xml文件。其中一些特别是嵌套的,如下所示:

 <emailAddresses>
            <emailAddress>
                <value>sambj1981@gmail.com</value>
                <typeSource>WORK</typeSource>
                <typeUser></typeUser>
                <primary>false</primary>
            </emailAddress>
            <emailAddress>
                <value>sambj@hotmail.co.uk</value>
                <typeSource>HOME</typeSource>
                <typeUser></typeUser>
                <primary>true</primary>
            </emailAddress>
        </emailAddresses>
String emailpath = "//emailAddress";
String emailvalue = ".//value";

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
Document document;
public XpathStuff(String file) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = docFactory.newDocumentBuilder();

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    document = builder.parse(bis);

    NodeList nodeList = getNodeList(document, emailpath);
    for(int i = 0; i < nodeList.getLength(); i++){
        System.out.println(getValue(nodeList.item(i), emailvalue));
    }
    bis.close();        
}

public NodeList getNodeList(Document doc, String expr) {
    try {
        XPathExpression pathExpr = xpath.compile(expr);
        return (NodeList) pathExpr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}


//extracts the String value for the given expression
private String getValue(Node n, String expr) {
    try {
        XPathExpression pathExpr = xpath.compile(expr);
        return (String) pathExpr.evaluate(n,
                XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

sambj1981@gmail.com
工作
错误的
sambj@hotmail.co.uk
家
符合事实的
在上面的节点中,我要做的是遍历每个节点并获取其中的值(value、typeSource、typeUser等),然后将它们放在POJO中

我试图看看是否可以使用这个xpath表达式
“//emailAddress”
,但它没有返回其中的标记。也许我做错了。我对使用xpath非常陌生

我可以这样做:

//emailAddress/value |//emailAddress/typeSource |..
但如果我没有弄错的话,那么这样做将把所有元素值都列在一起,让我在完成从特定emailAddress标记的读取并转到下一个emailAddress标记时再计算

为了总结我的需求,我基本上希望返回类似于从bog标准sql查询返回结果的方式,该查询返回一行结果。i、 e.如果您的sql查询生成10个emailAddress,它将返回一行中的每个emailAddress,我可以简单地迭代“每个emailAddress”,并根据colunm名称或索引获得适当的值

//emailAddress/*
将按文档顺序获取这些节点

这取决于您希望如何迭代节点。我们所有的XML都使用XOM(http://www.xom.nu/)这是一个简单可靠的Java包。可以使用XOM调用编写自己的策略

将按文档顺序获取这些节点


这取决于您希望如何迭代节点。我们所有的XML都使用XOM(http://www.xom.nu/)这是一个简单可靠的Java包。使用XOM调用编写自己的策略是可能的。

< P>我完全意识到这不是你要的,但可以考虑使用。这是一个用于人类可读的XML到POJO映射的工具。
所以我相信你可以快速地为你的邮件结构生成映射,让JiBX为你工作。

< P>我完全意识到这不是你要的,但可以考虑使用。这是一个用于人类可读的XML到POJO映射的工具。
因此,我相信您可以快速生成电子邮件结构的映射,并让jibx为您完成这项工作。

如果您使用XStream,您可以非常轻松地进行设置。像这样:

@XStreamAlias( "EmailAddress" )
public class EmailAddress {

   @XStreamAlias()
   private String value;

   @XStreamAlias()
   private String typeSource;

   @XStreamAlias()
   private String typeUser;

   @XStreamAlias()
   private boolean primary;

   // ... the rest omitted for brevity
}
然后,您可以非常简单地进行封送和解封:

XStream xstream = new XStream();
xstream.processAnnotations( EmailAddress.class );
xstream.toXML( /* Object value here */ emailAddress );
xstream.fromXML( /* String xml value here */ "" );

IDK,如果你必须使用XPath,但如果不是,我会考虑这样的一个盒子外的解决方案。

如果你使用XScript,你可以很容易地设置它。像这样:

@XStreamAlias( "EmailAddress" )
public class EmailAddress {

   @XStreamAlias()
   private String value;

   @XStreamAlias()
   private String typeSource;

   @XStreamAlias()
   private String typeUser;

   @XStreamAlias()
   private boolean primary;

   // ... the rest omitted for brevity
}
然后,您可以非常简单地进行封送和解封:

XStream xstream = new XStream();
xstream.processAnnotations( EmailAddress.class );
xstream.toXML( /* Object value here */ emailAddress );
xstream.fromXML( /* String xml value here */ "" );
IDK如果您必须使用XPath或不,但如果不是,我会考虑这样的一个框外解决方案。

< P>不,

//电子邮件地址

没有返回里面的标签,这是正确的。它返回的是节点列表/节点集。要实际获取值,可以执行以下操作:

 <emailAddresses>
            <emailAddress>
                <value>sambj1981@gmail.com</value>
                <typeSource>WORK</typeSource>
                <typeUser></typeUser>
                <primary>false</primary>
            </emailAddress>
            <emailAddress>
                <value>sambj@hotmail.co.uk</value>
                <typeSource>HOME</typeSource>
                <typeUser></typeUser>
                <primary>true</primary>
            </emailAddress>
        </emailAddresses>
String emailpath = "//emailAddress";
String emailvalue = ".//value";

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
Document document;
public XpathStuff(String file) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = docFactory.newDocumentBuilder();

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    document = builder.parse(bis);

    NodeList nodeList = getNodeList(document, emailpath);
    for(int i = 0; i < nodeList.getLength(); i++){
        System.out.println(getValue(nodeList.item(i), emailvalue));
    }
    bis.close();        
}

public NodeList getNodeList(Document doc, String expr) {
    try {
        XPathExpression pathExpr = xpath.compile(expr);
        return (NodeList) pathExpr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}


//extracts the String value for the given expression
private String getValue(Node n, String expr) {
    try {
        XPathExpression pathExpr = xpath.compile(expr);
        return (String) pathExpr.evaluate(n,
                XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}
String emailpath=“//emailAddress”;
字符串emailvalue=“.//value”;
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathFactory.newXPath();
文件;
公共XpathStuff(字符串文件)抛出ParserConfiguration异常、IOException、SAXException{
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=docFactory.newDocumentBuilder();
BufferedInputStream bis=新BufferedInputStream(新文件输入流(文件));
document=builder.parse(bis);
NodeList NodeList=getNodeList(文档,emailpath);
for(int i=0;i
也许我应该指出,当在Nodelist上迭代时,在//值中,第一个点表示当前上下文。如果没有点,您将始终获得第一个节点。

//电子邮件地址

没有返回里面的标签,这是正确的。它返回的是节点列表/节点集。要实际获取值,可以执行以下操作:

 <emailAddresses>
            <emailAddress>
                <value>sambj1981@gmail.com</value>
                <typeSource>WORK</typeSource>
                <typeUser></typeUser>
                <primary>false</primary>
            </emailAddress>
            <emailAddress>
                <value>sambj@hotmail.co.uk</value>
                <typeSource>HOME</typeSource>
                <typeUser></typeUser>
                <primary>true</primary>
            </emailAddress>
        </emailAddresses>
String emailpath = "//emailAddress";
String emailvalue = ".//value";

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
Document document;
public XpathStuff(String file) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = docFactory.newDocumentBuilder();

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    document = builder.parse(bis);

    NodeList nodeList = getNodeList(document, emailpath);
    for(int i = 0; i < nodeList.getLength(); i++){
        System.out.println(getValue(nodeList.item(i), emailvalue));
    }
    bis.close();        
}

public NodeList getNodeList(Document doc, String expr) {
    try {
        XPathExpression pathExpr = xpath.compile(expr);
        return (NodeList) pathExpr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}


//extracts the String value for the given expression
private String getValue(Node n, String expr) {
    try {
        XPathExpression pathExpr = xpath.compile(expr);
        return (String) pathExpr.evaluate(n,
                XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}
String emailpath=“//emailAddress”;
字符串emailvalue=“.//value”;
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathFactory.newXPath();
文件;
公共XpathStuff(字符串文件)抛出ParserConfiguration异常、IOException、SAXException{
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=docFactory.newDocumentBuilder();
BufferedInputStream bis=新BufferedInputStream(新文件输入流(文件));
document=builder.parse(bis);
NodeList NodeList=getNodeList(文档,emailpath);
for(int i=0;i