解析curl_exec()结果PHP

时间:2016-07-11 07:21:43

标签: php xml

我正在使用curl_exec()与Shipping Company Web服务合作。 查询结果为:

<?xml version="1.0" encoding="utf-8"?>
    <DataSet xmlns="#Oca_Express_Pak">
      <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="Table">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Tarifador" type="xs:int" minOccurs="0" />
                    <xs:element name="Precio" type="xs:decimal" minOccurs="0" />
                    <xs:element name="idTiposervicio" type="xs:int" minOccurs="0" />
                    <xs:element name="Ambito" type="xs:string" minOccurs="0" />
                    <xs:element name="PlazoEntrega" type="xs:int" minOccurs="0" />
                    <xs:element name="Adicional" type="xs:decimal" minOccurs="0" />
                    <xs:element name="Total" type="xs:decimal" minOccurs="0" />
                    <xs:element name="XML" type="xs:string" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:schema>
      <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
        <NewDataSet xmlns="">
          <Table diffgr:id="Table1" msdata:rowOrder="0">
            <Tarifador>23</Tarifador>
            <Precio>383.4100</Precio>
            <idTiposervicio>3</idTiposervicio>
            <Ambito>Nacional 1</Ambito>
            <PlazoEntrega>9</PlazoEntrega>
            <Adicional>0.0000</Adicional>
            <Total>383.4100</Total>
            <XML>&lt;row Tarifador="23" Precio="383.4100"/&gt;</XML>
          </Table>
        </NewDataSet>
      </diffgr:diffgram>
    </DataSet>

我如何解析结果以获得字段“PlazoEntrega”和“Total”?

谢谢你的支持!

1 个答案:

答案 0 :(得分:-1)

你可以使用preg_match()。

    public class DBHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "diet.db";

    public static final String TABLE_NAME = "diet";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_SPINNER_ITEM = "item";
    public static final String COLUMN_DATE = "date";
    public static final String COLUMN_TIME = "time";
    public static final String COLUMN_MENU = "menu";

    public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( "+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , "+
            COLUMN_DATE + " TEXT , "+COLUMN_MENU +" TEXT , " + COLUMN_TIME + " TEXT  ) ";


    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

}

    public class DBHelper2 {
    private DBHelper dbHelper;


    public DBHelper2(Context context){
        dbHelper = new DBHelper(context);
    }

    public void Delete(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(DBHelper.TABLE_NAME,null,null);
        db.close();
    }

    public int insert(String date,String time,String menu){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(DBHelper.COLUMN_DATE,date);
        values.put(DBHelper.COLUMN_TIME,time);
        values.put(DBHelper.COLUMN_MENU,menu);

        long insert = db.insert(DBHelper.TABLE_NAME,null,values);
        db.close();
        return (int) insert;
    }

    public List<String> getAllInfo(){
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String query = "SELECT * FROM "+ DBHelper.TABLE_NAME;
        List<String> allInfo = new ArrayList<String>();
        Cursor cursor = db.rawQuery(query,null);
        String datepicker;
        String timepicker;
        String menu;

        if(cursor.moveToFirst()){
            do {
                datepicker = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DATE));
                timepicker = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TIME));
                menu = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_MENU));

                allInfo.add(datepicker);
                allInfo.add(timepicker);
                allInfo.add(menu);
            }while (cursor.moveToNext());

        }
        return allInfo;
    }

}

PHP确实有一个XML解析器:

//$text = your curl result.
    preg_match('/<PlazoEntrega>(.*)<\/PlazoEntrega>/',$text,$matches);
    $plazo = $matches[1];
    preg_match('/<Total>(.*)<\/Total>/',$text,$matches);
    $total = $matches[1];