I have a text file that contains xpaths from some other (source) xml file. But, now I want to use those xpaths to create my custom xml file .

Below are files as above :-

Source.xml

<?xml version="1.0" encoding="UTF-8"?>
<html>
    <head>
        <title> My Page</title>
    </head>
    <body>
        <a href="abc.html">HomePage</a>
        <form id="my form" name="form 22" class="forms">
            <table id="table1">
                <tbody>
                    <tr>
                        <td>
                           <a> UserName </a> 
                           <input type="textbox" id="username" name="uname" class="login"/>
                        </td>
                    </tr>
                    <tr>
                         <td>
                           <a> Password </a> 
                           <input type="textbox" id="password" name="pwd" class="login"/>
                        </td>  
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
</html>

Generated xpaths using java stored in text file:-

source-xpaths.txt

//html[1]/head[1]/title[1]="My Page"


//html[1]/body[1]/a[1][@href='abc.html']
//html[1]/body[1]/a[1]="HomePage"


//html[1]/body[1]/form[1][@id='my form']
//html[1]/body[1]/form[1][@name='form 22']
//html[1]/body[1]/form[1][@class='forms']
//html[1]/body[1]/form[1]/table[1][@id='table1']
//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/a[1]="UserName"


//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/input[1][@type='textbox']
//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/input[1][@id='username']
//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/input[1][@name='uname']
//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/input[1][@class='login']
//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[2]/td[1]/a[1]="Password"


//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[2]/td[1]/input[1][@type='textbox']
//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[2]/td[1]/input[1][@id='password']
//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[2]/td[1]/input[1][@name='pwd']
//html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[2]/td[1]/input[1][@class='login']

Now, I want my custom xml to be formed from this xpaths as below:-

<My Page>
</My Page>

<HomePage>
    <href>abc.html</href>
</HomePage>

<UserName>
    <type>textbox</type>
    <id>username</id>
    <name>uname</name>
    <class>login</class>
</UserName>

<Password>
    <type>textbox</text>
    <id>password</id>
    <name>pwd</name>
    <class>login</class>
</Password>

//etc etc....

In short, I want all text in or tags to be parent node and all corresponding attributes to be their child nodes.

How can I create custom xml in java?

Any help is appreciated... :)

有帮助吗?

解决方案

You will want to apply an XSL transformation to your Source.xml file. An XSL file makes use of XPath expressions such as yours.

Java supports XSL transformations using the javax.xml.transform package:

File sourceFile = new File("Source.xml");
File customXmlFile = new File("custom.xml");

Source source = new StreamSource(sourceFile);
Result result = new StreamResult(customXmlFile);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();

transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION. "yes");
transformer.setOutputProperty(OutputKeys.INDENT. "yes");

transformer.transform(source, result);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top