xml conversion using php

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
VlearnsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
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
MokonaCommented:
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
MokonaCommented:
@gr8gonzo why use external lib when php already has simpleXML? And your method for creating the xml isn't very clean.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

gr8gonzoConsultantCommented:
@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
MokonaCommented:
@gr8gonzo

Yes not always only 95%
0
gr8gonzoConsultantCommented:
@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
gr8gonzoConsultantCommented:
@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
VlearnsAuthor Commented:
hi experts

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

regards
0
MokonaCommented:
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
gr8gonzoConsultantCommented:
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
VlearnsAuthor Commented:
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
gr8gonzoConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MokonaCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.