Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

xml conversion using php

Posted on 2009-07-06
13
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 35

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

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 35

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
 
LVL 35

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 35

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 35

Accepted Solution

by:
gr8gonzo earned 1000 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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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