小编Sah*_*pta的帖子

如何在Java中使用xpath在xml中查找节点值或属性并将其替换为其他值?

这是我的代码到目前为止:

// locate the node(s)
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xpath.evaluate("//root[text()='input[1]']", doc, XPathConstants.NODESET);

// make the change
for (int idx = 0; idx < nodes.getLength(); idx++) {
    nodes.item(idx).setTextContent(input[3]);
}

// save the result
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
Run Code Online (Sandbox Code Playgroud)

输入[1]是我在XML中寻找的东西,输入[3]就是它被替换的内容.

这是XML:

<root> 
    <accounts> 
        <account name="Bill Gates">
            <position>CEO</position> 
            <phoneNumber>123-485-1854</phoneNumber>
            <notes>Runs the company</notes> 
        </account> 
        <account name="Service Account"/> 
        <account name="Burt Mackland"> 
            <position>CFO</position> 
            <phoneNumber>345-415-4813</phoneNumber> 
            <notes>Great CFO</notes> </account> 
        <account name="Joe Smith"> 
            <position>Accountant</position>
            <reportsTo>Burt Mackland</reportsTo>
            <phoneNumber>135-118-7815</phoneNumber> 
            <notes>Must have ID checked at …
Run Code Online (Sandbox Code Playgroud)

java xml xpath

6
推荐指数
1
解决办法
9999
查看次数

如何将背景图像添加到QMainWindow?

嗨,我是QT创作者的新手.我已经尝试了很多东西来设置Q主窗口的背景图像.我在图片中添加了一个资源文件夹.我尝试在UI中使用setstylesheet添加,并尝试编码.当我使用UI时,我可以看到图像,但是当我运行它时,没有任何显示.我想把一张扑克桌的图像作为背景,并且能够将按钮等放在上面.

main.cpp中:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();


    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setStyleSheet("{background-image: url(:/images/images/PokerTableBackground.jpg);}");
}

MainWindow::~MainWindow()
{
    delete ui;
}
Run Code Online (Sandbox Code Playgroud)

就像我说的那样,我尝试过这样做,并将图像放在UI中,而且其中任何一个都不起作用.我想将图像设置为整个事物的背景.

我也试过用这个:

QWidget *pic = new QWidget(ui->tab);
    pic->setStyleSheet("background-image: url(:/images/images/PokerTableBackground.jpg)");
    pic->setGeometry(QRect(10,10,220,48)); // your location and size.
Run Code Online (Sandbox Code Playgroud)

c++ qt

6
推荐指数
3
解决办法
4万
查看次数

在尝试调用方法时,如何使用Lambda MetaFactory或Method Handle而不是反射?

我有一个方法,假设将类名,参数和方法名称作为参数,并根据它找到的类调用该方法.方法:

Method[] methods = className.getDeclaredMethods();
 for (Method meth: methods) {
  if (meth.getName().equalsIgnoreCase(methodName)) {
   try {
    MethodType mt = MethodType.methodType(boolean.class, String.class);
    MethodHandles.Lookup caller = MethodHandles.lookup();
    MethodHandle handle = caller.findVirtual(Utilities.class, meth.getName(), mt);
    /*
     * Trying to invoke using lambdaMetaFactory
     */
    MethodType methodType = MethodType.methodType(boolean.class);
    MethodType invokedType = MethodType.methodType(BooleanSupplier.class);
    /*
     * Throws java.lang.invoke.LambdaConversionException: Incorrect number of parameters for instance method 
     * invokeVirtual com.grainger.Automation.Utilities.navigateToUrl:(String)boolean; 0 captured parameters, 
     * 0 functional interface method parameters, 1 implementation parameters
     */
    CallSite site = LambdaMetafactory.metafactory(caller, "getAsBoolean", invokedType, methodType, …
Run Code Online (Sandbox Code Playgroud)

java reflection

2
推荐指数
1
解决办法
1987
查看次数

标签 统计

java ×2

c++ ×1

qt ×1

reflection ×1

xml ×1

xpath ×1