[英]How to create a customized POJO class from XML file?
I have an XML file which is like this. 我有一个像这样的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>
Reading this XML file, I need to generate a POJO class which will look like this. 读取此XML文件,我需要生成一个看起来像这样的POJO类。
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;
}
Basically, the page
attribute of elements
tag appended with the string "PO" should be the class name. 基本上,添加了字符串“ PO”的elements
标签的page
属性应该是类名。 The key
attribute of element
tag must be the member variables. element
标签的key
属性必须是成员变量。 Depending on the findBy
attribute value (ID/XPATH), corresponding attribute must be added in the annotation @FindBy()
根据findBy
属性值(ID / XPATH),必须在注释@FindBy()
添加相应的属性。
Please help! 请帮忙!
Using XSLT: 使用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>
processing: 处理:
$ xsltproc transform.xsl input.xml $ xsltproc transform.xsl input.xml
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.