Solved

remove carraige returns from xml doc

Posted on 2004-04-28
6
618 Views
Last Modified: 2013-12-13
I created and xml file (see below)and it displays in notepad as follows, there is carriage returns after every row.
How can remove these cr when I create my xml file.

I generate my xml file as follows. Most the code is modified code from suggestions from this site.  I use this code to generate
an xml using an online application at datareformatics.com, I can upload a csv file and then convert it to xml.  I have written code to do the reverse to import an xml file and convert to csv. However with carriage return in xml file the import fails. I'm not sure whether I should be stripping the cr on export or on import.
Thanks in advance.

$doc    = domxml_new_doc("1.0");
$data   = $doc->create_element("data");
   
for ($row_ct =1;$row_ct <= $clean_row_num ;$row_ct++)
{//ROW COUNTER LOOP
               
$row                 = $doc->create_element("row");
$row_elements  = array();

$file_row = fgetcsv( $file_handle,filesize($upload_dir.$file_nm), $import_delim);
 
 for ($col_num =0;$col_num < $initial_col_hdr_ct ;$col_num++)
 {//COLUMN COUNTER LOOP
         
  for ($i =0;$i < $final_col_hdr_ct ;$i++)
  {//LOOP FINAL COL
               
        $element_nm           = str_replace(" ", "_", $export_col_nm[$i]);//Places underscore between COL NAME                        
        $row_elements[$element_nm]  = $doc->create_element($element_nm);
                                 
        while ($dpc_row = mysql_fetch_array($select_dpc_result[$i]))
        {//WHILE LOOP
           //LOTS OF LOGIC IN HERE  to calculate $last_value[$i]                  
                         
        }//WHILE LOOP
                 
/**************************************************************************************
Add Support here for foreign chars such as accents, umlauts etc. php does not support these chars
in default charset so need to encode as UTF-8 and then when writing the file decode back to
IS-8559-1 charset i.e default charset. dump_mem(true, 'ISO-8859-1')
**************************************************************************************/

        $last_value[$i] = mb_convert_encoding($last_value[$i],'UTF-8','ISO-8859-1');                      
        $last_value[$i] = $doc->create_text_node($last_value[$i]);
        $row_elements[$element_nm]->append_child($last_value[$i]);        
        $row->append_child($row_elements[$element_nm]);


  }//END LOOP FINAL COL
       
$data->append_child($row);
         
 }//END COLUMN COUNTER LOOP
                               
}//ROW COUNTER LOOP

    $doc->append_child($data);
 
    $output_string = $doc->dump_mem(true, 'ISO-8859-1');
//Write File etc

  <?xml version="1.0" encoding="ISO-8859-1"?>
<data>
  <row>
    <ShipperID>1</ShipperID>
    <CompanyName>Speedy Express</CompanyName>
    <Phone>(503) 555-9831</Phone>
  </row>
  <row>
    <ShipperID>2</ShipperID>
    <CompanyName>United Package</CompanyName>
    <Phone>(503) 555-3199</Phone>
  </row>
  <row>
    <ShipperID>3</ShipperID>
    <CompanyName>Federal Shipping</CompanyName>
    <Phone>(503) 555-9931</Phone>
  </row>
  <row>
    <ShipperID>4</ShipperID>
    <CompanyName>Big Ships</CompanyName>
    <Phone>(503) 555-9932</Phone>
  </row>
  <row>
    <ShipperID>5</ShipperID>
    <CompanyName>B+I Shipping</CompanyName>
    <Phone>(503) 555-9933</Phone>
  </row>
  <row>
    <ShipperID>6</ShipperID>
    <CompanyName>International Shipping</CompanyName>
    <Phone>(503) 555-9934</Phone>
  </row>
  <row>
    <ShipperID>7</ShipperID>
    <CompanyName>AAA Shipping</CompanyName>
    <Phone>(503) 555-9935</Phone>
  </row>
  <row>
    <ShipperID>8</ShipperID>
    <CompanyName>NIPON Shipping</CompanyName>
    <Phone>(503) 555-9936</Phone>
  </row>
</data>
0
Comment
Question by:dplinnane
  • 4
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
ID: 10948217
2 answers.

1 tested ...

$sSearchFor = '|(\r\n *)|i';
$sReplaceWith = '';

$new_output_string= preg_replace($sSearchFor,$sReplaceWith,$output_string);


1 not tested ...

$output_string = $doc->dump_mem(false, 'ISO-8859-1');

Regards,

Richard.
0
 

Author Comment

by:dplinnane
ID: 10951013
I tried the second one and it works perfectly. I will award the points to you after a bitmore testing.
I have another qustion, is there anyway I could verify that a file I am upload is in the format above without the carriage returns. The file can have any number of columns, it should always be a data file.  I have an application which allow users to upload their files and clean/format and convert them to an export file type, either csv, fixed width, and insert statement.
I want to insure that the xml file they are uploading will not crash my app because it is in a correct fromat. I'll postthis as a new question once I hear back from you.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 10957131
What you are looking for is a way to compare an XML file against a XSD / DTD (I think that's the right acronyms).

From what I know, an XSD is an XML file which describes an XML file.

e.g.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="version_control" type="version_controlType"/>
  <xs:complexType name="version_controlType">
    <xs:sequence>
      <xs:element name="applications" type="applicationsType"/>
      <xs:element name="notifications" type="notificationsType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="applicationsType">
    <xs:sequence>
      <xs:element name="application" type="applicationType" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="applicationType">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="current_version" type="xs:decimal"/>
      <xs:element name="url" type="xs:string"/>
      <xs:element name="regx" type="xs:string"/>
      <xs:element name="download_url" type="xs:string"/>
      <xs:element name="save_location" type="xs:string"/>
      <xs:element name="notify" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="notificationsType">
    <xs:sequence>
      <xs:element name="notification" type="notificationType" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="notificationType">
    <xs:sequence>
      <xs:element name="notify" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>


This file describes a file I use to hold data about application version numbers and who should be contacted when the version numbers change.

The above file was produced using a small Delphi app.

I do not know (at this stage) how to validate an XML vs XSD/DTD. The XML parser within PHP does not validate XML files.

I suspect you will need to use an external tool to validate them.

I did find ...

http://codingtheweb.users.phpclasses.org/browse.html/package/608.html

which is ...

"This class implements the Schematron 1.5 language to validate XML documents, it uses the XSLT PHP extension and can validate XML files using Schematron XML files or compiled Schematron scripts as XSLT. Many different ways to validate files/uris/etc are provided."

Maybe this will do what you want.

Richard.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:RQuadling
ID: 11004669
Any joy?
0
 

Author Comment

by:dplinnane
ID: 11015705
I'll look into the above but you will see if you check out the site that I can not use an external tool it has to be built in as part of of our app. For now I have put simple checks in place as follows. $row_elements[0]->name   =='#text' this checks if carriage returns exist after each row element, this will cause a failure so I exit and send a message to the browser that the file is invalid. As I mentioned in another post you can check it out at.  I'm sure there is other logic I could add which looks for row and data tags. For now I will have to use this technique as the above mentioned is beyond my abilities right now.
https://datareformatics.com/index_login.php?guest_email=guest@datareformatics.com&guest_pw=guest123&is_guest=yes


 $doc = xmldocfile("$upload_dir$file_nm");
   
    if (!$doc = xmldocfile("$upload_dir$file_nm"))
    {
   
    return  "Error in XML document $upload_dir$file_nm";
   
    }
    elseif ($doc->version > 1.0)
    {
   
    return  'Unsupported XML version';

    }
           
    $doc = domxml_open_file("$upload_dir$file_nm");

    $rows = $doc->get_elements_by_tagname("row");
   
    util_dr_print_r ($rows,'rows get_elements_by_tagname', $function_nm, __LINE__);

/**************************************************************************************
comment:
Sample 5 rows and if no errors return true which mean the file is ok.
**************************************************************************************/        
    foreach($rows as $row)
    {
    $row_ct++;
   
    $row_elements = $row->child_nodes();

    util_dr_print_r ($row_elements,'row_elements xml', $function_nm, __LINE__);

        if($row_elements[0]->name   =='#text')
        {
       
        return  'XML file format is invalid';
           
        }
        else
        {

            if($row_ct   > '5')
            {
           
            return  'true';
               
            }
       
        }
       
    }
   
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 11015990
I think validating an XML file against a schema is going to be quite tricky in just PHP.

If you need this sort of thing, talk to the hoster. Most can add in functionality if required.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Paypal 502 Error 3 63
Could you possibly point a workaround on this jQuery/ plugin datatable error? 5 39
updating the date data 12 19
is this a cms? 8 32
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now