Solved

xml conversion using php

Posted on 2009-07-06
13
326 Views
Last Modified: 2012-05-07
hey

<?xml version="1.0" encoding="utf-8"?>
<Result>
<tag1>content</tag1>
<tag2>content</tag2>
</Result>
 
what i need to output is
 
<?xml version="1.0" encoding="utf-8"?>
<tag1>content</tag1>
</Result>
 
how do i do that using php
0
Comment
Question by:Vlearns
  • 6
  • 5
  • 2
13 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 24790818
I'm assuming that's just an example and the real thing is more complicated? If so, you could try using the PHP Simple HTML DOM class:

http://simplehtmldom.sourceforge.net/

I've included an example below using your code.
<?

include("simple_html_dom.php");

$html = str_get_html('<?xml version="1.0" encoding="utf-8"?>

<Result>

<tag1>content 1</tag1>

<tag2>content 2</tag2>

</Result>');
 

$newXML = '<?xml version="1.0" encoding="utf-8"?><Result>';

foreach($html->find('tag1') as $tag1)

{

	$newXML .= $tag1->outertext;

}

$newXML .= "</Result>";

print $newXML;

?>

Open in new window

0
 
LVL 1

Expert Comment

by:Mokona
ID: 24790833
This method uses simplexml and DOMDocument.
<?php

$string = <<<XML

<?xml version="1.0" encoding="utf-8"?>

<Result>

	<tag1>content</tag1>

	<tag2>content</tag2>

</Result>

XML;
 

$xml = simplexml_load_string($string);

$newXml = new DOMDocument('1.0', 'utf-8');
 

$root = $newXml->createElement('Result');

$element = $newXml->createElement('tag1', $xml->tag1);

$root->appendChild($element);

$newXml->appendChild($root);
 

echo $newXml->saveXML();
 

/*

Output:

<?xml version="1.0" encoding="utf-8"?>

<Result>

	<tag1>content</tag1>

</Result>

*/

Open in new window

0
 
LVL 1

Expert Comment

by:Mokona
ID: 24790854
@gr8gonzo why use external lib when php already has simpleXML? And your method for creating the xml isn't very clean.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 24790901
@Mokona:

1. ...because he/she provided a generic example, so I'm figuring that the real thing might be more complex, and the simple HTML DOM class is extremely flexible when it comes to selecting specific tags. Plus, sometimes the PHP installation doesn't have the XML libs installed, and the class I suggested does not rely on the XML extension. (Although, I'll grant that it's USUALLY installed. Just not ALWAYS.)

2. It wasn't intended to be. The class I suggested has a cleaner way of generating XML, but again, I was trying to customize my answer for the asker's potential needs so he could make it simpler or more complex as appropriate.



0
 
LVL 1

Expert Comment

by:Mokona
ID: 24790972
@gr8gonzo

Yes not always only 95%
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 24791061
@Mokona:

-frown- You might want to think about losing the attitude. The focus of EE is to help the person asking the question. We're here to do that first and foremost, not to stomp on each other's answers. I know you just registered today, but experts tend to respect each other and be polite when they disagree. It makes for a much better environment for asking questions, answering questions, and also for learning.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 34

Expert Comment

by:gr8gonzo
ID: 24791076
@Mokona:

Regarding 95% - it doesn't really matter how high the percentage is if the person asking the question is in the percentage that doesn't have it.

Now, I'm not sure where you got 95% from, but I'm guessing it's a much lower percentage. The reason I think that is because server admins love to try to maximize performance by eliminating all the extensions that are not commonly used (lower memory footprint = more available RAM for serving more concurrent connections), and XML is often one of the first to go.

Shared hosting is really the one scenario where the XML extension is nearly guaranteed to be installed, but again, we don't know about his hosting situation. Just saying.
0
 

Author Comment

by:Vlearns
ID: 24791572
hi experts

thanks for the replies
can we use standard php functions
my workplace does not allow external libraries to be installed

regards
0
 
LVL 1

Expert Comment

by:Mokona
ID: 24792260
There you go, Cold Turkey  style
<?php

$string = <<<XML

<?xml version="1.0" encoding="utf-8"?>

<Result>

        <tag1>content</tag1>

        <tag2>content</tag2>

</Result>

XML;
 

function getXmlTag($xml, $tagname) {

	preg_match('@<' . $tagname . '[^>]*>(.*)<\/' . $tagname . '>@i', $xml, $tag);

	return 	$tag[1];

}
 

$tag = getXmlTag($string, "tag1");

$newXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

$newXml .= "<Result>";

$newXml .= "<tag1>" . $tag . "</tag1>";

$newXml .= "</result>";	
 

echo $newXml;

	

?>

Open in new window

0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 24793729
The simple HTML DOM solution I proposed at the beginning also uses standard PHP functions.

Please let us know if you're really searching for "<tag1>" or something more specific. It will help us give you a better answer if we can see a better example. Thanks!
0
 

Author Comment

by:Vlearns
ID: 24795922
HI Experts,

thaks again for all your inputs.

here is the answer to your question

input file is a superset of xml tags. like tag 1 .....to tag n
output file is a subset  say tag2 tag3 etc

I only know the subset i want to output,  like tag2 tag3 etc

i am not aware of what extra superficial tags may be present in the input file.

the problem is...

i have a specific xml i want to output (i know what xml tags i need to)

the input to my program is a superset xml file...with extra superficial tags, that i dont control.


My program should only select the xml tags i need  and omit everything else

for example

$array_of_tags_i_know_are_needed = ("tag1",tag2");

input xml
<?xml version="1.0" encoding="utf-8"?>
<Result>
<tag1>content</tag1>
<tag2>content</tag2>
<tag3>content</tag3>
<tag4>content</tag4>
</Result>
 
what i need to output is
 
<?xml version="1.0" encoding="utf-8"?>
<Result>
<tag1>content</tag1>
<tag2>content</tag2>
</Result>

i only have tag1 and tag2 in the list which i knew i needed, i have omitted all other tags




0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 24796564
A slight modification to my first example, then:

<?
include("simple_html_dom.php");
$html = str_get_html('<?xml version="1.0" encoding="utf-8"?>
<Result>
<tag1>content 1</tag1>
<tag2>content 2</tag2>
<tag3>content 3</tag3>
<tag4>content 4</tag4>
</Result>');
 
$array_of_tags_i_know_are_needed = ("tag1",tag2");
$newXML = '<?xml version="1.0" encoding="utf-8"?><Result>';
foreach($array_of_tags_i_know_are_needed as $tagID)
{
foreach($html->find($tagID) as $tag)
{
      $newXML .= $tag->outertext;
}
}
$newXML .= "</Result>";
print $newXML;
?>
0
 
LVL 1

Assisted Solution

by:Mokona
Mokona earned 250 total points
ID: 24797212
Modification to my last example
$string = <<<XML

<?xml version="1.0" encoding="utf-8"?>

<Result>

        <tag1>content</tag1>

        <tag2>content</tag2>

</Result>

XML;
 

$array_of_tags_i_know_are_needed = array(

	"tag1", 

	"tag2"

);
 

function getXmlTag($xml, $tagname) {

        preg_match('@<' . $tagname . '[^>]*>(.*)<\/' . $tagname . '>@i', $xml, $tag);

        return  $tag[1];

}
 

$tag = getXmlTag($string, "tag1");

$newXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

$newXml .= "<Result>";
 

foreach($array_of_tags_i_know_are_needed as $tagNeeded)

	$newXml .= "<$tagNeeded>" . getXmlTag($string, $tagNeeded) . "</$tagNeeded>";
 

$newXml .= "</result>"; 

 

echo $newXml;

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now