开发者

how i can use SAX parser

开发者 https://www.devze.com 2022-12-31 12:33 出处:网络
This is what the result should look like when i parse it through a SAX parser http://img13.imageshack.us/img13/6950/75914446.jpg

This is what the result should look like when i parse it through a SAX parser http://img13.imageshack.us/img13/6950/75914446.jpg This is the XML source code from which i need to generate the display:

<orders>
  <order>
    <count>37</count>
    <price>49.99</price>
    <book>
      <isbn>0130897930</isbn>
      <title>Core Web Programming Second Edition</title>
      <authors>
         <count>2</count>
        <author>Marty Hall</author>
        <author>Larry Brown</author>
      </authors>
    </book>
  </order>
  <order>
    <count>1</count>
    <price>9.95</price>
    <yacht>
      <manufacturer>Luxury Yachts, Inc.</manufacturer>
      <model>M-1</model>
      <standardFeatures oars="plastic" lifeVests="none">false</standardFeatures>
</yacht>
  </order>
  <order>
    <count>3</count>
    <price>22.22</price>
    <book>
      <isbn>B000059Z4H</isbn>
      <title>Harry Potter and the Order of the Phoenix</title>
      <authors>
         <count>1</count>
        <author>J.K. Rowling</author>
      </authors>
    </book>
  </order>

i really have no clue how to code the functions but i have just set up the parser

$xmlParser =开发者_如何学运维 xml_parser_create("UTF-8");
xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($xmlParser, 'startElement', 'endElement');
xml_set_character_data_handler($xmlParser, 'HandleCharacterData');
$fileName = 'orders.xml';
    if (!($fp = fopen($fileName, 'r'))){
        die('Cannot open the XML file: ' . $fileName);
    }
    while ($data = fread($fp, 4096)){
        $parsedOkay = xml_parse($xmlParser, $data, feof($fp));
        if (!$parsedOkay){
            print ("There was an error or the parser was finished.");
            break;
        }
    }
    xml_parser_free($xmlParser);

    function startElement($xmlParser, $name, $attribs)
    {
    }

    function endElement($parser, $name)
    {
    }

    function HandleCharacterData($parser, $data)
    {
    }


A SimpleXML-based recursive approach might be a simpler solution perhaps, something like:

  $xml = new SimpleXmlElement($data);

  function renderTree(SimpleXmlElement $xml, $depth = 0) {
    $output = str_repeat('| ', $depth);
    $output .= '+-' . $xml->getName();

    if (sizeof($xml->attributes()) > 0) {
      $attrs = '';
      foreach ($xml->attributes() as $key => $value) {
        $attrs .= $key . '=' . $value . ', ';
      }
      $output .= ' (' . trim($attrs, ', ') . ')';
    }

    $output .= ': ' . (string)$xml;
    $output .= '<br />';

    if ($xml->count() > 0) {
      foreach ($xml->children() as $child) {
        $output .= renderTree($child, $depth + 1);
      }
    }

    return $output;
  }

  echo renderTree($xml);

Would render the tree as in the example image.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号