Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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 如何从XML文件创建定制的POJO类?_Java_Xml_Xsd_Jaxb_Pojo - Fatal编程技术网

Java 如何从XML文件创建定制的POJO类?

Java 如何从XML文件创建定制的POJO类?,java,xml,xsd,jaxb,pojo,Java,Xml,Xsd,Jaxb,Pojo,我有一个类似这样的XML文件 <?xml version="1.0" encoding="UTF-8"?> <elements area="Login" page="Login" description="Description about the generated class"> <element key="USERNAMETEXTBOX" findBy="ID" id="username" tag="input" name="" text=""

我有一个类似这样的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<elements area="Login" page="Login" description="Description about the generated class">    
    <element key="USERNAMETEXTBOX" findBy="ID" id="username" tag="input" name="" text="" xPath="//*[@id='username']"/>     
    <element key="PASSWORDTEXTBOX" findBy="ID" id="password" tag="input" name="" text="" xPath="//*[@id='password']"/>
    <element key="LOGINBUTTON" findBy="XPATH" id="" tag="button" name="" text="" xPath="//input[@value='LOGIN']"/>
</elements>
public class LoginPO extends CommonActionHelper{

    LoginBean loginBeanObj= new LoginBean();
    private WebDriver driver;

    @FindBy(id="username")
    private WebElement USERNAME;

    @FindBy(id="password")
    private WebElement PASSWORD;

    @FindBy(xpath="//input[@value='LOGIN']")
    private WebElement LOGIN;    
}
基本上,附加字符串PO的elements标签的page属性应该是类名。元素标记的键属性必须是成员变量。根据findBy属性值ID/XPATH,必须在注释@findBy中添加相应的属性

请帮忙

使用XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="elements"/>
</xsl:template>

<xsl:template match="elements">
public class <xsl:value-of select="@area"/>PO extends CommonActionHelper{
 <xsl:value-of select="@area"/>Bean <xsl:value-of select="translate(@area,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>BeanObj= new  <xsl:value-of select="@area"/>Bean();
private WebDriver driver;
<xsl:apply-templates select="element"/>
}
</xsl:template>


<xsl:template match="element[@findBy='ID']">
@FindBy(id="<xsl:value-of select="@id"/>")
private WebElement <xsl:apply-templates select="." mode="variable"/>;
</xsl:template>

<xsl:template match="element[@findBy='XPATH']">
@FindBy(xpath="<xsl:value-of select="@xPath"/>")
private WebElement <xsl:apply-templates select="." mode="variable"/>;
</xsl:template>

<xsl:template match="element" mode="variable">
<xsl:choose>
    <xsl:when test="substring(@key, string-length(@key) - 6) ='TEXTBOX'"><xsl:value-of select="substring(@key,1,string-length(@key)-7)"/></xsl:when>
    <xsl:when test="substring(@key, string-length(@key) - 5) ='BUTTON'"><xsl:value-of select="substring(@key,1,string-length(@key)-6)"/></xsl:when> <xsl:otherwise>!!<xsl:value-of select="@key"/></xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

你可以试试apachevelocity@RogerDwan是否有我可以参考的示例项目或代码?这会有很大的帮助。我发现这对初学者来说并不难理解。
public class LoginPO extends CommonActionHelper{
 LoginBean loginBeanObj= new  LoginBean();
private WebDriver driver;

@FindBy(id="username")
private WebElement USERNAME;

@FindBy(id="password")
private WebElement PASSWORD;

@FindBy(xpath="//input[@value='LOGIN']")
private WebElement LOGIN;

}