其兄弟值查找XML節點值我有以下XML文件具有使用XPath
<?xml version="1.0"?>
<mappings>
<enumMapping id="1" dsrName="yesno" emName="yesno_t">
<valueMap>
<dsrValue>Yes</dsrValue>
<emValue>1</emValue>
</valueMap>
<valueMap>
<dsrValue>No</dsrValue>
<emValue>2</emValue>
</valueMap>
</enumMapping>
<enumMapping id="2" dsrName="altRoutingOnConFailure" emName="Alternate_Routing_On_Connection_Failure_t">
<valueMap>
<dsrValue>Same Peer</dsrValue>
<emValue>1</emValue>
</valueMap>
<valueMap>
<dsrValue>Different Peer</dsrValue>
<emValue>2</emValue>
</valueMap>
<valueMap>
<dsrValue>Same Connection</dsrValue>
<emValue>3</emValue>
</valueMap>
</enumMapping>
</mappings>
和Java代碼
public class Parser {
Document doc;
public Parser(String filename)
{
try{
File inputFile = new File(filename);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
}catch(Exception e)
{
e.printStackTrace();
}
}
public void searchDsrEnum(String dsrName,String dsrValue)
{
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
try {
XPathExpression expr = xpath.compile("mappings/enumMapping[@dsrName=\""+dsrName+"\"]/valueMap/dsrValue");
NodeList n1=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
System.out.println(n1.getLength());
//System.out.println(n1.);
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我需要找到emValue
領域,隨着dsrValue
= Yes
和enumMapping
屬性dsrName=yesno
使用xpath
。如果我使用 我發現了一個錯誤
XPathExpression expr = xpath.compile("mappings/enumMapping[@dsrName=\""+dsrName+"\"]/valueMap//[dsrValue/text()=\""+dsrValue+"\"]/emValue");
你有什麼錯誤? –
我懷疑錯誤是關於無效的XPath表達式,因爲這種表達式是無效的XPath:'valueMap // [...]'。也許OP的意思是'// valueMap [...]'... – har07
javax.xml.transform.TransformerException:在'/'或'//'標記之後應該有一個位置步驟。 \t在com.sun.org.apache.xpath.internal.compiler.XPathParser.error(未知來源) \t在com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(未知來源) \t at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source) \t at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source) \t at –