XML Parse Help
Posted: Mon May 02, 2005 8:41 pm
Hello
I'm trying to parse an XML file, but I'm having some trouble. I'm pretty much a newb when it comes to XML, so any help is greatly appreciated.
Below is a snip of my XML file. It is a bibliography export from a program called EndNote. I need to pull values from this XML file and dump them into a MySQL database, so I'm trying to understand how to reference these values.
So the problem I'm running into, is that I can't seem to nail a few of these elements. Basically, any tags without any attributes are 'lost' to my parser??. contributors, authors, author, titles, title, etc. So what I get is :
The items I need to pull are basically...
Author
Title
Year
Pub-Dates
Related-URLs
Here is my parser code below.. it's rough...
Any help is greatly appreciated!
thanks
hanji
I'm trying to parse an XML file, but I'm having some trouble. I'm pretty much a newb when it comes to XML, so any help is greatly appreciated.
Below is a snip of my XML file. It is a bibliography export from a program called EndNote. I need to pull values from this XML file and dump them into a MySQL database, so I'm trying to understand how to reference these values.
Code: Select all
<?xml version="e;1.0"e; encoding="e;UTF-8"e; ?>
<xml>
<records>
<record>
<database name="e;test.enl"e; path="e;E:\test\bibliography\test.enl"e;>test.enl</database>
<source-app name="e;EndNote"e; version="e;8.0"e;>EndNote</source-app>
<rec-number>3</rec-number>
<ref-type name="e;Online Multimedia"e;>48</ref-type>
<contributors>
<authors>
<author>
<style face="e;normal"e; font="e;default"e; size="e;100%"e;>Los Alamos National Labs Chemistry Division</style>
</author>
</authors>
</contributors>
<titles>
<title>
<style face="e;normal"e; font="e;default"e; size="e;100%"e;>test</style>
</title>
</titles>
<dates>
<year>
<style face="e;normal"e; font="e;default"e; size="e;100%"e;>2003</style>
</year>
<pub-dates>
<date>
<style face="e;normal"e; font="e;default"e; size="e;100%"e;>April 12, 2005</style>
</date>
</pub-dates>
</dates>
<urls>
<related-urls>
<url>
<style face="e;normal"e; font="e;default"e; size="e;100%"e;>http://test.lanl.gov/periodic/test/1.html</style>
</url>
</related-urls>
</urls>
</record>
</records>
</xml>Code: Select all
XML
RECORDS
RECORD
DATABASE
SOURCE-APP
REC-NUMBER
REF-TYPE
CONTRIBUTORS
AUTHORS
AUTHOR
STYLE
1 =>Los Alamos National Labs Chemistry Division
TITLES
TITLE
STYLE
2 =>test
DATES
YEAR
STYLE
3 =>2003
PUB-DATES
DATE
STYLE
4 =>April 12, 2005
URLS
RELATED-URLS
URL
STYLE
5 =>http://test.lanl.gov/test/elements/1.htmlAuthor
Title
Year
Pub-Dates
Related-URLs
Here is my parser code below.. it's rough...
Code: Select all
<?
$variable_data = array();
$current_tag = "";
$variable_label = "";
$variable_type = "";
function start_element($parser, $element_name, $element_attr) {
global $current_tag, $variable_label, $variable_type,$count;
$current_tag = $element_name;
switch($element_name){
case "DATABASE":
$count = 1;
break;
}
echo $element_name."<br>";
}
function end_element($parser, $element_name) {
global $current_tag, $variable_type;
$current_tag = "";
$variable_type = "";
}
function character_data($parser, $data) {
global $current_tag, $variable_label,$count;
switch($current_tag){
case "STYLE":
echo $count." =>".$data."<br>";
$count++;
break;
}
}
$file = "biblio042505.xml";
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_set_character_data_handler($parser, "character_data");
$fp = fopen($file,'r');
while($data = fread($fp,4096)){
$count = 1;
xml_parse($parser,str_replace("'","'",$data),feof($fp));
}
xml_parser_free($parser);
?>thanks
hanji