Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to converting an array to a XML(has attributes) ? #48

Open
zishiguo opened this issue Sep 14, 2018 · 1 comment
Open

How to converting an array to a XML(has attributes) ? #48

zishiguo opened this issue Sep 14, 2018 · 1 comment

Comments

@zishiguo
Copy link

zishiguo commented Sep 14, 2018

If xml likes:

<urlset content_method="full"> 
    <url>
        <loc>http://www.example.com/a.html</loc>
        <desc>a.html</desc>
    </url> 
    <url>
        <loc>http://www.example.com/b.html</loc>
        <desc>b.html</desc>
    </url>
</urlset>

Then, the array is ?

@zishiguo zishiguo reopened this Sep 15, 2018
@zsping1989
Copy link

        $data = [
            'urlset' => [
                '@attributes' => [
                    'content_method' => 'full'
                ],
                [
                    '@name' => 'url',
                    'loc' => 'http://www.example.com/a.html',
                    'desc' => 'a.html'
                ], [
                    '@name' => 'url',
                    'loc' => 'http://www.example.com/b.html',
                    'desc' => 'b.html'
                ]
            ]
        ];
        $request_xml = Formatter::make($data, Formatter::ARR)->toXml('xml','utf-8',true);
        dd($request_xml);

结果

<?xml version="1.0" encoding="utf-8"?>
<xml>
    <urlset content_method="full">
        <url>
           <loc>http://www.example.com/a.html</loc>
           <desc>a.html</desc>
       </url> 
       <url>
            <loc>http://www.example.com/b.html</loc>
           <desc>b.html</desc>
       </url>
    </urlset>
</xml>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants