在XML文件中获取特定的子名称

时间:2014-04-01 16:04:44

标签: java xml jdom jdom-2

我有一个像这样的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<ClOrdIDS><ClOrdID id="1"><Account>1005390</Account><Symbol>SAP</Symbol><SecurityID>4663789</SecurityID><SecurityExchange>XETR</SecurityExchange><Price>23.0</Price><Order_Type>Limit</Order_Type><Side>SELL</Side><Order_Quantity>0.001</Order_Quantity></ClOrdID><ClOrdID id="2"><Account>1005390</Account><Symbol>SAP</Symbol><SecurityID>4663789</SecurityID><SecurityExchange>XETR</SecurityExchange><Price>13.0</Price><Order_Type>Limit</Order_Type><Side>SELL</Side><Order_Quantity>0.001</Order_Quantity></ClOrdID><ClOrdID id="3"><Account>1005390</Account><Symbol>SAP</Symbol><SecurityID>4663789</SecurityID><SecurityExchange>XETR</SecurityExchange><Price>13.0</Price><Order_Type>Limit</Order_Type><Side>BUY</Side><Order_Quantity>0.001</Order_Quantity></ClOrdID><ClOrdID id="4"><Account>1005390</Account><Symbol>SAP</Symbol><SecurityID>4663789</SecurityID><SecurityExchange>XETR</SecurityExchange><Price>13.0</Price><Order_Type>Limit</Order_Type><Side>BUY</Side><Order_Quantity>0.001</Order_Quantity></ClOrdID><ClOrdID id="5"><Account>1005390</Account><Symbol>SAP</Symbol><SecurityID>4663789</SecurityID><SecurityExchange>XETR</SecurityExchange><Price>13.0</Price><Order_Type>Limit</Order_Type><Side>BUY</Side><Order_Quantity>0.001</Order_Quantity></ClOrdID><ClOrdID id="6"><Account>1005390</Account><Symbol>SAP</Symbol><SecurityID>4663789</SecurityID><SecurityExchange>XETR</SecurityExchange><Price>13.0</Price><Order_Type>Limit</Order_Type><Side>BUY</Side><Order_Quantity>0.001</Order_Quantity></ClOrdID><ClOrdID id="7"><Account>1005390</Account><Symbol>SAP</Symbol><SecurityID>4663789</SecurityID><SecurityExchange>XETR</SecurityExchange><Price>13.0</Price><Order_Type>Limit</Order_Type><Side>SELL</Side><Order_Quantity>0.001</Order_Quantity></ClOrdID></ClOrdIDS>

如何使用ClOrdID id =“3”提取子元素?

感谢

3 个答案:

答案 0 :(得分:2)

你可以这样做

String xmlString = ... 
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(xmlString);

XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
String xpathExp = "/ClOrdIDS/ClOrdID[@id=\"3\"]";
NodeList childNodeList = (NodeList) xpath.evaluate(xpathExp, doc, XPathConstants.NODESET);

答案 1 :(得分:1)

由于问题上的标签是JDOM而不是DOM,你可以改用JDOM ;-):

Document doc = new SaxBuilder().build(xmlFile);
XPathExpression<Element> xpe = XPathFactory.instance()
          .compile("/ClOrdIDS/ClOrdID[@id=\"3\"]", Filters.element());
List<Element> idThrees = xpe.evaluate(doc);

答案 2 :(得分:0)

以下是在VTD-XML ...

中执行此操作的代码
import com.ximpleware.*;


public class removeElement {
    public static void main(String s[]) throws VTDException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/ClOrdIDS/ClOrdID[@id='3']");
        int i=0;
        while((i=ap.evalXPath())!=-1){

        }
    }
}