Php xml Conversion to array Functions

Source: Internet
Author: User

Php xml Conversion to array Functions

  1. <? Php
  2. Function xml2array ($ originalXML, $ attributes = true)
  3. {
  4. $ XmlArray = array ();
  5. $ Search = $ attributes? | <(S) (. *) s *> (. *) </<? Php
    Function xml2array ($ originalXML, $ attributes = true)
    {
    $ XmlArray = array ();
    $ Search = $ attributes? | <(S )(. *) s *> (. *) </2> | Ums: | <(S )()). *> (. *) </2> | Ums;

    // Normalize data
    $ Xml = preg_replace (|> s * <|, "> <", $ originalXML); // one tag per line
    $ Xml = preg_replace (| <?. *?> |, $ Xml); // remove XML declarations
    $ Xml = preg_replace (| <(S ?) (. *)/> | U, <$1 $2> </$1>, $ xml); // Expand singletons

    If (! Preg_match_all ($ search, $ xml, $ xmlMatches ))
    Return trim ($ originalXML); // bail out-no XML found

    Foreach ($ xmlMatches [1] as $ index => $ key)
    {
    If (! Isset ($ xmlArray [$ key]) $ xmlArray [$ key] = array ();
    $ XmlArray [$ key] [] = xml2array ($ xmlMatches [4] [$ index], $ attributes );
    }
    Return $ xmlArray;
    }
    ?> | Ums: | <(S) (). *> (. *) </<? Php
    Function xml2array ($ originalXML, $ attributes = true)
    {
    $ XmlArray = array ();
    $ Search = $ attributes? | <(S )(. *) s *> (. *) </2> | Ums: | <(S )()). *> (. *) </2> | Ums;

    // Normalize data
    $ Xml = preg_replace (|> s * <|, "> <", $ originalXML); // one tag per line
    $ Xml = preg_replace (| <?. *?> |, $ Xml); // remove XML declarations
    $ Xml = preg_replace (| <(S ?) (. *)/> | U, <$1 $2> </$1>, $ xml); // Expand singletons

    If (! Preg_match_all ($ search, $ xml, $ xmlMatches ))
    Return trim ($ originalXML); // bail out-no XML found

    Foreach ($ xmlMatches [1] as $ index => $ key)
    {
    If (! Isset ($ xmlArray [$ key]) $ xmlArray [$ key] = array ();
    $ XmlArray [$ key] [] = xml2array ($ xmlMatches [4] [$ index], $ attributes );
    }
    Return $ xmlArray;
    }
    ?> | Ums;
  6. // Normalize data
  7. $ Xml = preg_replace (|> s * <|, "> <", $ originalXML); // one tag per line
  8. $ Xml = preg_replace (| <?. *?> |, $ Xml); // remove XML declarations
  9. $ Xml = preg_replace (| <(S ?) (. *)/> | U, <[code] <? Php
    Function xml2array ($ originalXML, $ attributes = true)
    {
    $ XmlArray = array ();
    $ Search = $ attributes? | <(S )(. *) s *> (. *) </2> | Ums: | <(S )()). *> (. *) </2> | Ums;

    // Normalize data
    $ Xml = preg_replace (|> s * <|, "> <", $ originalXML); // one tag per line
    $ Xml = preg_replace (| <?. *?> |, $ Xml); // remove XML declarations
    $ Xml = preg_replace (| <(S ?) (. *)/> | U, <$1 $2> </$1>, $ xml); // Expand singletons

    If (! Preg_match_all ($ search, $ xml, $ xmlMatches ))
    Return trim ($ originalXML); // bail out-no XML found

    Foreach ($ xmlMatches [1] as $ index => $ key)
    {
    If (! Isset ($ xmlArray [$ key]) $ xmlArray [$ key] = array ();
    $ XmlArray [$ key] [] = xml2array ($ xmlMatches [4] [$ index], $ attributes );
    }
    Return $ xmlArray;
    }
    ?> </[Code]>, $ xml); // Expand singletons
  10. If (! Preg_match_all ($ search, $ xml, $ xmlMatches ))
  11. Return trim ($ originalXML); // bail out-no XML found
  12. Foreach ($ xmlMatches [1] as $ index => $ key)
  13. {
  14. If (! Isset ($ xmlArray [$ key]) $ xmlArray [$ key] = array ();
  15. $ XmlArray [$ key] [] = xml2array ($ xmlMatches [4] [$ index], $ attributes );
  16. & Nb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.