使用Powershell搜索xmls

时间:2015-09-04 19:30:48

标签: xml powershell

我如何使用powershell查找此XML中的路径?例如,我需要找到Application / Name = PSTest AND Environment / Name = DEMO

的路径

以下是XML示例:

    <Applications>
        <Application>
            <Name>PSTest</Name>
            <Environment>
                <Name>DEMO</Name>
                <BuildPath>C:\TEST\TEST</BuildPath>
                <Path>C:\TEST\DEMO\PSTest|C:\TEST\DEMO1\PSTest</Path>
                <BackupPath>C:\TEST\Backup</BackupPath>
            </Environment>
            <Environment>
                <Name>PROD</Name>
                <BuildPath>C:\TEST\DEMO</BuildPath>
                <Path>C:\TEST\PROD\PSTest|C:\TEST\PROD1\PSTest</Path>
                <BackupPath>C:\TEST\Backup\PROD</BackupPath>
            </Environment>
        </Application>  
        <Application>
            <Name>PSTestTwo</Name>
            <Environment>
                <Name>DEMO</Name>
                <BuildPath>C:\TEST\TEST</BuildPath>
                <Path>C:\TEST\DEMO\PSTestTwo|C:\TEST\DEMO1\PSTestTwo</Path>
                <BackupPath>C:\TEST\Backup</BackupPath>
            </Environment>
            <Environment>
                <Name>PROD</Name>
                <BuildPath>C:\TEST\DEMO</BuildPath>
                <Path>C:\TEST\PROD\PSTestTwo|C:\TEST\PROD1\PSTestTwo</Path>
                <BackupPath>C:\TEST\Backup\PROD</BackupPath>
            </Environment>
        </Application>  
    </Applications>

1 个答案:

答案 0 :(得分:1)

您可以使用XPath字符串查找和选择节点:

public void getInputFromUser ()
{

    String input = null;
    JTextField textField = new JTextField();
    textField.setColumns(50);

    textField.setVisible(true);


    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    frame.setSize(300, 75);
    frame.add(textField);
    frame.setVisible(true);
    frame.requestFocus();
    frame.addWindowListener(null);


    textField.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent ae){

              String text = textField.getText();

              System.out.println(text);
           }
        });

请记住,与PowerShell本身不同,XPath区分大小写。